gofr-dev/gofr · error

client not connected, call Connect() first

Error message

client not connected, call Connect() first

What it means

errClientNotConnected is returned by the DynamoDB kv-store client's Get, Set, Delete, Subscribe, Publish, and Query methods when the client has not been initialized via Connect(). The client guards every operation so calls before connection fail fast with this message instead of nil-pointer panics.

Source

Thrown at pkg/gofr/datasource/kv-store/dynamodb/dynamo.go:19

package dynamodb

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

var (
	errClientNotConnected = errors.New("client not connected, call Connect() first")
	errKeyNotFound        = errors.New("key not found")
	errStatusDown         = errors.New("status down")
)

type Configs struct {
	Table            string
	Region           string
	Endpoint         string
	PartitionKeyName string
}
type dynamoDBInterface interface {
	PutItem(
		ctx context.Context,
		params *dynamodb.PutItemInput,
		optFns ...func(*dynamodb.Options),
	) (*dynamodb.PutItemOutput, error)
	GetItem(
		ctx context.Context,

View on GitHub (pinned to 187eb24962)

Solutions

  1. Call client.Connect(config) before any operation
  2. Check the connection state / add errors.Is(err, errClientNotConnected) handling
  3. Ensure Close is only called at shutdown and clients are re-created afterwards
  4. Await successful Connect (or readiness check) before serving traffic

Example fix

// before
client := &dynamo.Client{}
client.Get(ctx, "key") // not connected
// after
client := &dynamo.Client{}
if err := client.Connect(cfg); err != nil {
    return err
}
client.Get(ctx, "key")
Defensive patterns

Strategy: validation

Validate before calling

if client == nil || !client.connected {
    return errors.New("dynamo client not initialized; call Connect()")
}
client.Get(ctx, key)

Type guard

func isConnected(c *dynamo.Client) bool {
    return c != nil // plus internal connected flag if exposed
}

Try / catch

if err := client.Set(ctx, k, v); err != nil {
    if err.Error() == "client not connected, call Connect() first" {
        _ = client.Connect(cfg)
        err = client.Set(ctx, k, v)
    }
    return err
}

Prevention

When it happens

Trigger: Calling any store method (Get/Set/Delete/Query/Publish/Subscribe) before Connect() was called, or after Close() without reconnecting; Connect() failing silently and code proceeding anyway.

Common situations: Forgetting Connect in setup; connection deferred to a goroutine while requests arrive; app restarts where Close ran but handlers still reference the old client; test fixtures that skip Connect.

Related errors


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