gofr-dev/gofr · error

error marshaling data

Error message

error marshaling data

What it means

While walking a dotted path, a segment key is not present in the current map (or a non-map was hit mid-path and it wasn't the last segment). The library wraps errClaimPathNotFound with the deepest resolved prefix (parts[:i+1]) to pinpoint where traversal stopped. The token parsed successfully; the configured nested path just doesn't exist in it.

Source

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

	"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.
type Client struct {
	config  Config
	client  *es.Client
	logger  Logger
	metrics Metrics

View on GitHub (pinned to 187eb24962)

Solutions

  1. Decode a real token and correct the nested path to the actual key chain
  2. Require the nested claim in token issuance for all relevant clients/flows
  3. errors.Is(err, errClaimPathNotFound) → return 401/403 and log the exact failing prefix shown in the message

Example fix

// before
extractClaimValue(claims, "permisions.role") // typo
// after
extractClaimValue(claims, "permissions.role")
Defensive patterns

Strategy: try-catch

Validate before calling

if _, ok := claims["permissions"]; !ok {
    return fmt.Errorf("nested claim permissions missing from token")
}

Type guard

func hasNestedPath(claims jwt.MapClaims, parts ...string) bool {
    cur := claims
    for i, p := range parts {
        next, ok := cur[p].(map[string]any)
        if !ok && i < len(parts)-1 { return false }
        if i == len(parts)-1 { _, ok := cur[p]; return ok }
        cur = jwt.MapClaims(next)
    }
    return false
}

Try / catch

v, err := extractClaimValue(claims, "permissions.role")
if errors.Is(err, errClaimPathNotFound) {
    http.Error(w, "missing claim: "+err.Error(), http.StatusUnauthorized)
    return
}

Prevention

When it happens

Trigger: Nested path like "permissions.role" where "permissions" or "permissions.role" key is absent from the token; non-final segment resolving to a non-map value (handled here as the non-last-branch of the default case).

Common situations: Typo in nested claim path; IdP omits the nested object for some token types (client-credentials tokens often lack user claims); renaming claims during migration.

Related errors


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