gofr-dev/gofr · error

FTP config is nil

Error message

FTP config is nil

What it means

errFTPConfigNil is returned by the FTP storage adapter's Connect when its internal *Config pointer is nil. The adapter cannot dial an FTP server without connection parameters, so it refuses to start. It is a programming/configuration error, not a transient network condition.

Source

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

package ftp

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"io"
	"path"
	"strings"
	"time"

	"github.com/jlaffaye/ftp"
	"gofr.dev/pkg/gofr/datasource/file"
)

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 {

View on GitHub (pinned to 187eb24962)

Solutions

  1. Provide a non-nil *ftp.Config with host/port before connecting.
  2. Guard at startup: if cfg == nil, fail fast with a clear configuration error instead of reaching Connect().
  3. Use ftp.New(&ftp.Config{...}) so defaults (DialTimeout, location) are applied.

Example fix

// before
adapter := &ftp.StorageAdapter{} // cfg nil
adapter.Connect()
// after
adapter := ftp.New(&ftp.Config{Host: host, Port: 21})
Defensive patterns

Strategy: validation

Validate before calling

if cfg == nil {
    return nil, fmt.Errorf("ftp config required: set host and port")
}
fs := ftp.New(cfg)

Type guard

func hasFTPConfig(c *ftp.Config) bool { return c != nil && c.Host != "" }

Try / catch

// Connect is async and logs; guard construction instead
if cfg == nil {
    log.Fatal("FTP config is nil; refusing to start FTP filesystem")
}
fs := ftp.New(cfg)

Prevention

When it happens

Trigger: Calling Connect() on a storageAdapter created with a nil cfg (e.g. ftp.New(nil) paths where config was never populated, or manual adapter construction with &storageAdapter{cfg: nil}).

Common situations: Passing nil to constructors and assuming defaults; zero-value struct usage before initialization; DI frameworks injecting a nil config when no FTP config is present in the environment.

Related errors


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