gofr-dev/gofr · error

document ID cannot be empty

Error message

document ID cannot be empty

What it means

The bracketed segment must be a numeric index; extractArrayClaim parses it with fmt.Sscanf("%d"). If parsing fails (non-numeric or empty inside brackets) it wraps errInvalidArrayIndex with the offending fragment. This guarantees the array notation refers to an actual positional element.

Source

Thrown at pkg/gofr/datasource/elasticsearch/elasticsearch.go:26

	"fmt"
	"strings"
	"time"

	es "github.com/elastic/go-elasticsearch/v8"
	"github.com/elastic/go-elasticsearch/v8/esapi"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

const (
	statusDown     = "DOWN"
	statusUp       = "UP"
	defaultTimeout = 5 * time.Second
)

var (
	errEmptyIndex        = errors.New("index name cannot be empty")
	errEmptyDocumentID   = errors.New("document ID cannot be empty")
	errEmptyQuery        = errors.New("query cannot be empty")
	errEmptyOperations   = errors.New("operations cannot be empty")
	errHealthCheckFailed = errors.New("elasticsearch health check failed")
	errOperation         = errors.New("elasticsearch operation error")
	errMarshaling        = errors.New("error marshaling data")
	errParsingResponse   = errors.New("error parsing response")
	errResponse          = errors.New("invalid elasticsearch response")
	errEncodingOperation = errors.New("error encoding operation")
)

// Config holds the configuration for connecting to Elasticsearch.
type Config struct {
	Addresses []string
	Username  string
	Password  string
}

// Client represents the Elasticsearch client.

View on GitHub (pinned to 187eb24962)

Solutions

  1. Use a numeric zero-based index, e.g. "roles[0]"
  2. If you need key-based access, use dotted nested-path syntax ("claims.roles.primary") rather than bracket notation
  3. Sanitize/atoi the index at config load time so invalid values fail at boot, not per-request

Example fix

// before
extractClaimValue(claims, "roles[admin]")
// after
extractClaimValue(claims, "roles[0]")
Defensive patterns

Strategy: validation

Validate before calling

idxStr := strings.Trim(strings.SplitN(path, "[", 2)[1], "]")
if _, err := strconv.Atoi(idxStr); err != nil {
    return fmt.Errorf("claim path index must be numeric: %q", idxStr)
}

Try / catch

v, err := extractClaimValue(claims, path)
if errors.Is(err, errInvalidArrayIndex) {
    // correct to numeric index before retrying
}

Prevention

When it happens

Trigger: Paths like "roles[]", "roles[abc]", "roles[first]"; dynamic index strings built from user input or config that weren't converted to integers.

Common situations: Using a named key where an index is required (wanting roles[admin] instead of nested claims); config value stored as a string enum instead of a number; copying dot-notation JSON-path habits into this simpler syntax.

Related errors


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