gofr-dev/gofr · error

azure config is nil

Error message

azure config is nil

What it means

storage_adapter.Connect(ctx, cfg) requires a non-nil *Config; if cfg is nil it returns the sentinel errAzureConfigNil — "azure config is nil". The adapter cannot build the azfile service client or share client without connection parameters, so Connect fails immediately rather than returning an adapter whose client is unusable. This is distinct from the fs.go errInvalidConfig family: it guards the Connect lifecycle, not New.

Source

Thrown at pkg/gofr/datasource/file/azure/storage_adapter.go:22

	"bytes"
	"context"
	"errors"
	"fmt"
	"io"
	"mime"
	"path/filepath"
	"strings"
	"time"

	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/directory"
	azfile "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/file"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share"
	"gofr.dev/pkg/gofr/datasource/file"
)

var (
	// Storage adapter errors.
	errAzureConfigNil            = errors.New("azure config is nil")
	errAzureClientNotInitialized = errors.New("azure client or share 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")
	errSameSourceOrDest          = errors.New("source and destination are the same")
	errFailedToCreateReader      = errors.New("failed to create reader")
	errFailedToCreateRangeReader = errors.New("failed to create range reader")
	errObjectNotFound            = errors.New("object not found")
	errFailedToGetProperties     = errors.New("failed to get properties")
	errFailedToDeleteObject      = errors.New("failed to delete object")
	errFailedToCopyObject        = errors.New("failed to copy object")
	errFailedToListObjects       = errors.New("failed to list objects")
	errFailedToListDirectory     = errors.New("failed to list directory")
	errWriterAlreadyClosed       = errors.New("writer already closed")
	errInvalidWhence             = errors.New("invalid whence")
	errNegativeOffset            = errors.New("negative offset")
	errShareNameEmpty            = errors.New("share name cannot be empty")
)

View on GitHub (pinned to 187eb24962)

Solutions

  1. Pass a fully populated *Config (ShareName, AccountName, AccountKey, endpoints) to Connect.
  2. Fix config loading order — load and validate configuration before the datasource Connect lifecycle runs.
  3. If using the higher-level azure.New, ensure its Config is built before FileSystem.Connect is invoked.
  4. Add a nil check/panic-early in your wiring code so the misconfiguration surfaces at boot with clear context.

Example fix

// before
var cfg *Config
fs.Connect(ctx, cfg) // panics into errAzureConfigNil
// after
cfg := &Config{ShareName: "files", AccountName: acc, AccountKey: key}
if err := fs.Connect(ctx, cfg); err != nil {
    log.Fatalf("azure files connect failed: %v", err)
}
Defensive patterns

Strategy: validation

Validate before calling

if cfg == nil {
    return errors.New("azure config must be loaded before Connect")
}

Type guard

func canConnect(cfg *azure.Config) bool {
    return cfg != nil && cfg.ShareName != "" && cfg.AccountName != "" && cfg.AccountKey != ""
}

Try / catch

if err := adapter.Connect(ctx, cfg); err != nil {
    if strings.Contains(err.Error(), "config is nil") {
        log.Fatal("azure storage adapter: config was nil at Connect — check config loading order")
    }
    return err
}

Prevention

When it happens

Trigger: Calling Connect(ctx, nil) on the Azure storage adapter — e.g. wiring the FileSystem without loading config first, a framework hook passing a nil config interface, or a struct field of pointer type left as its zero value.

Common situations: App startup where datasource config loading is skipped or fails silently; test scaffolding calling Connect without config; refactors changing config from value to pointer type leaving nil defaults; conditional config loading branches that never ran.

Related errors


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