{"record":{"id":"bf46d5a5c89e4027","repo":"gofr-dev/gofr","slug":"ftp-client-is-not-initialized","errorCode":null,"errorMessage":"FTP client is not initialized","messagePattern":"FTP client is not initialized","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/file/ftp/storage_adapter.go","lineNumber":20,"sourceCode":"\nimport (\n\t\"bytes\"\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"path\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/jlaffaye/ftp\"\n\t\"gofr.dev/pkg/gofr/datasource/file\"\n)\n\nvar (\n\t// Storage adapter errors.\n\terrFTPConfigNil            = errors.New(\"FTP config is nil\")\n\terrFTPClientNotInitialized = errors.New(\"FTP client is not initialized\")\n\terrEmptyObjectName         = errors.New(\"object name is empty\")\n\terrInvalidOffset           = errors.New(\"invalid offset: must be >= 0\")\n\terrEmptySourceOrDest       = errors.New(\"source and destination names cannot be empty\")\n\terrSameSourceAndDest       = errors.New(\"source and destination are the same\")\n\terrFailedToCreateReader    = errors.New(\"failed to create reader\")\n\terrFailedToCreateWriter    = errors.New(\"failed to create writer\")\n\terrObjectNotFound          = errors.New(\"object not found\")\n\terrFailedToGetObjectAttrs  = errors.New(\"failed to get object attrs\")\n\terrFailedToDeleteObject    = errors.New(\"failed to delete object\")\n\terrFailedToListObjects     = errors.New(\"failed to list objects\")\n\terrFailedToListDirectory   = errors.New(\"failed to list directory\")\n\terrWriterAlreadyClosed     = errors.New(\"writer already closed\")\n\terrFTPConfigInvalid        = errors.New(\"invalid FTP configuration: host and port are required\")\n)\n\n// Config represents the FTP configuration.\ntype Config struct {\n\tHost        string        // FTP server hostname","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/file/ftp/storage_adapter.go#L2-L38","documentation":"errFTPClientNotInitialized is returned by adapter operations (NewReader, NewRangeReader, NewWriter, DeleteObject, CopyObject, StatObject) when the internal FTP client has not been connected yet. Operations require a live *ftp.ServerConn obtained during Connect; without it the adapter cannot talk to the server.","triggerScenarios":"Calling any object operation before Connect() succeeds, or after a failed/dropped connection left the client nil; also returned by TestStorageAdapter-type paths where the client was never set.","commonSituations":"Using the filesystem immediately after New without waiting for connection; background retry still in progress after the first Connect failure; server disconnect invalidated and nulled the client.","solutions":["Call fs.Connect() and wait until IsConnected() is true (or connection-ready signal) before performing file operations.","Check the connection state before each operation and re-Connect if needed.","Inspect Connect logs for the initial failure (bad host/credentials) that prevented client initialization."],"exampleFix":"// before\nfs := ftp.New(cfg)\nr, err := fs.Open(\"file.txt\") // client not initialized\n// after\nfs := ftp.New(cfg)\nfs.Connect()\n// wait for connection / check IsConnected()\nr, err := fs.Open(\"file.txt\")","handlingStrategy":"validation","validationCode":"func (a *App) withFTP(op func(fs file.FileSystemProvider) error) error {\n    if !a.ftpConnected.Load() { return fmt.Errorf(\"ftp not connected\") }\n    return op(a.fs)\n}","typeGuard":"func connected(fs *file.CommonFileSystem) bool { return fs.IsConnected() }","tryCatchPattern":"if !fs.IsConnected() {\n    fs.Connect()\n    // wait/subscribe for ready before proceeding\n}\nif err := op(ctx, fs); err != nil {\n    // re-Connect and retry once on transport failures\n}","preventionTips":["Gate all file operations behind a confirmed connection state.","Subscribe to connection-ready events instead of calling operations immediately after New.","Implement a reconnect wrapper that re-Connects on client-not-initialized errors.","Health-check the FTP connection in readiness probes."],"tags":["go","ftp","connection","lifecycle"],"backgroundTag":"client-not-initialized","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}