gofr-dev/gofr · warning

key not found

Error message

key not found

What it means

errKeyNotFound is the sentinel error ('key not found') declared in the DynamoDB KV-store client and returned by Get and Delete when the requested key has no entry in the configured DynamoDB table. The Get implementation wraps it with the key name using %w, so callers can detect it with errors.Is(err, errKeyNotFound). It is a normal, expected condition rather than a fault in the client.

Source

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

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,
		params *dynamodb.GetItemInput,

View on GitHub (pinned to 187eb24962)

Solutions

  1. Check errors.Is(err, datasource/.../dynamodb errKeyNotFound) and treat it as a miss, then Set or fall back
  2. Verify Configs.Table and Configs.Region point to the table/region where the key was written
  3. Confirm the key was Set successfully before Get (check Set's returned error)
  4. Use consistent key naming (constant or helper) to avoid typos/case mismatches

Example fix

// before
val, err := store.Get(ctx, "user:42")
if err != nil { return err }
// after
val, err := store.Get(ctx, "user:42")
if errors.Is(err, dynamodb.ErrKeyNotFound) {
    val = defaultUser // handle miss gracefully
} else if err != nil { return err }
Defensive patterns

Strategy: fallback

Validate before calling

// ensure table configured before use
if cfg.Table == "" || cfg.Region == "" { return errors.New("dynamodb KV not configured") }

Type guard

func IsKeyNotFound(err error) bool { return errors.Is(err, dynamodb.ErrKeyNotFound) }

Try / catch

v, err := store.Get(ctx, key)
switch {
case errors.Is(err, dynamodb.ErrKeyNotFound): v = missValue(key)
case err != nil: return err
}

Prevention

When it happens

Trigger: Calling Client.Get(ctx, key) on a key that was never Set, was Delete'd, or whose item lacks the 'value' attribute in the table; calling Delete with a non-existent key; querying the wrong table or wrong AWS region so the key appears missing.

Common situations: Typos or casing differences in keys, pointing Configs.Table at a stale or different table, reading before an initial Set (e.g. cache-miss logic), keys removed by TTL or by another process.

Related errors


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