gofr-dev/gofr · error

invalid FTP configuration: host and port are required

Error message

invalid FTP configuration: host and port are required

What it means

errFTPConfigInvalid is returned by Connect when the Config is missing a Host or Port, which are mandatory for dialing an FTP server. It is a fail-fast validation error raised before any network activity. It indicates a programming or configuration mistake, not a runtime condition.

Solutions

  1. Set Host and Port in the Config before calling Connect
  2. Load values from environment or config file and validate they are non-empty/non-zero
  3. Fail fast at application startup with an explicit validation check on the config
  4. Check config file keys/spelling so values actually populate the struct

Example fix

// before
cfg := &ftp.Config{} // Host, Port unset
fs.Connect(cfg)
// after
cfg := &ftp.Config{Host: "ftp.example.com", Port: 21, User: u, Password: p}
if cfg.Host == "" || cfg.Port == 0 { return errors.New("ftp host/port required") }
fs.Connect(cfg)
Defensive patterns

Strategy: validation

Validate before calling

func validateFTPConfig(cfg *ftp.Config) error {
    if cfg == nil || cfg.Host == "" || cfg.Port == 0 {
        return errors.New("ftp config: host and port are required")
    }
    return nil
}

Type guard

func isConfigInvalid(err error) bool { return errors.Is(err, ftp.ErrFTPConfigInvalid) }

Try / catch

if err := fs.Connect(cfg); errors.Is(err, ftp.ErrFTPConfigInvalid) {
    // config bug — do not retry; fix construction of Config
    return err
}

Prevention

When it happens

Trigger: Calling Connect with a Config whose Host is "" or whose Port is 0 — e.g. constructing Config{} without loading values, or environment variables/flags not wired into the struct.

Common situations: Missing FTP_HOST/FTP_PORT env vars at deploy time; config struct built with defaults that leave Port zero; YAML/JSON config key typos leaving fields unset.

Related errors


AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01). Data as JSON: /api/errors/55322867243341ea. Report an issue: GitHub.

Appendix: source

Thrown at pkg/gofr/datasource/file/ftp/storage_adapter.go:33

)

var (
	// Storage adapter errors.
	errFTPConfigNil            = errors.New("FTP config is nil")
	errFTPClientNotInitialized = errors.New("FTP client is not initialized")
	errEmptyObjectName         = errors.New("object name is empty")
	errInvalidOffset           = errors.New("invalid offset: must be >= 0")
	errEmptySourceOrDest       = errors.New("source and destination names cannot be empty")
	errSameSourceAndDest       = errors.New("source and destination are the same")
	errFailedToCreateReader    = errors.New("failed to create reader")
	errFailedToCreateWriter    = errors.New("failed to create writer")
	errObjectNotFound          = errors.New("object not found")
	errFailedToGetObjectAttrs  = errors.New("failed to get object attrs")
	errFailedToDeleteObject    = errors.New("failed to delete object")
	errFailedToListObjects     = errors.New("failed to list objects")
	errFailedToListDirectory   = errors.New("failed to list directory")
	errWriterAlreadyClosed     = errors.New("writer already closed")
	errFTPConfigInvalid        = errors.New("invalid FTP configuration: host and port are required")
)

// Config represents the FTP configuration.
type Config struct {
	Host        string        // FTP server hostname
	User        string        // FTP username
	Password    string        // FTP password
	Port        int           // FTP port
	RemoteDir   string        // Remote directory path. Base Path for all FTP Operations.
	DialTimeout time.Duration // FTP connection timeout
}

// storageAdapter adapts FTP client to implement file.StorageProvider.
type storageAdapter struct {
	cfg  *Config
	conn *ftp.ServerConn
}

View on GitHub (pinned to 187eb24962)