{"id":"141b6ba2cc1384d5","repo":"gofiber/fiber","slug":"fiber-failed-to-encode-shared-state-s-value-w","errorCode":null,"errorMessage":"fiber: failed to encode shared state %s value: %w","messagePattern":"fiber: failed to encode shared state (.+?) value: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared_state.go","lineNumber":368,"sourceCode":"\t\terr       error\n\t\trecovered any\n\t)\n\tfunc() {\n\t\t// App-configured codecs may be nil or may still use Fiber's\n\t\t// binder.Unimplemented* placeholders, which panic instead of returning an\n\t\t// error, so recover here and surface a regular error.\n\t\tdefer func() {\n\t\t\trecovered = recover()\n\t\t}()\n\n\t\tencoded, err = encoder(v)\n\t}()\n\n\tif recovered != nil {\n\t\treturn nil, sharedStateCodecPanicError(\"encode\", format, recovered)\n\t}\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"fiber: failed to encode shared state %s value: %w\", format, err)\n\t}\n\n\treturn encoded, nil\n}\n\nfunc decodeSharedStateValue(data []byte, out any, decoder func([]byte, any) error, format string) error {\n\tif decoder == nil {\n\t\treturn sharedStateCodecNotConfiguredError(format, \"decoder\")\n\t}\n\n\tvar (\n\t\terr       error\n\t\trecovered any\n\t)\n\tfunc() {\n\t\t// App-configured codecs may be nil or may still use Fiber's\n\t\t// binder.Unimplemented* placeholders, which panic instead of returning an\n\t\t// error, so recover here and surface a regular error.","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/shared_state.go#L350-L386","documentation":"Returned by encodeSharedStateValue (shared_state.go:368) when a configured encoder (JSON/XML/MsgPack/CBOR) returns a non-nil error while serializing a value for SetJSON/SetXML/SetMsgPack/SetCBOR. The format name (e.g. 'json') and the original encoder error are wrapped via %w so the caller can diagnose marshalling problems.","triggerScenarios":"Calling app.SharedState SetJSON/SetXML/SetMsgPack/SetCBOR with a value the configured encoder cannot serialize — e.g. a struct with unexported fields for JSON, a channel/func field, a recursive struct, a map with non-string keys, or a type missing XML tags.","commonSituations":"Custom JSONEncoder replacement that rejects types stdlib json accepts; msgpack/cbor encoders that do not support certain Go types; passing an interface{} holding an unserializable concrete type; version upgrade of the encoder library adding stricter validation.","solutions":["Reproduce the encode in isolation: json.Marshal(v) directly to see the exact marshal error.","Add the required struct tags (json:\"...\") / make fields exported / remove unsupported types (chan, func, complex).","If using a custom encoder (cfg.JSONEncoder etc.), verify it handles your type; fall back to json.Marshal to confirm.","For msgpack/cbor, register custom types or use a serializable representation."],"exampleFix":"// before: unserializable field\ntype S struct { fn func() }\n_ = state.SetJSON(ctx, \"k\", S{}, time.Minute)\n\n// after: serializable shape\ntype S struct { Name string `json:\"name\"` }\nif err := state.SetJSON(ctx, \"k\", S{Name: \"x\"}, time.Minute); err != nil {\n    return err\n}","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(v); err != nil {\n    return fmt.Errorf(\"pre-check encode failed: %w\", err)\n}\n_ = state.SetJSON(ctx, \"k\", v, ttl)","typeGuard":null,"tryCatchPattern":"if err := state.SetJSON(ctx, \"k\", v, ttl); err != nil {\n    if strings.Contains(err.Error(), \"failed to encode\") {\n        // serialization issue — fix the type or tags\n    }\n}","preventionTips":["Add struct tags for all serializable fields.","Avoid chan/func/complex/unexported fields in shared-state values.","Unit-test your types against the encoder before storing them."],"tags":["shared-state","serialization","encode","json","xml","msgpack","cbor"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}