{"id":"1c9b5ce13bfaf31b","repo":"gofiber/fiber","slug":"fiber-failed-to-decode-shared-state-s-value-w","errorCode":null,"errorMessage":"fiber: failed to decode shared state %s value: %w","messagePattern":"fiber: failed to decode shared state (.+?) value: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared_state.go","lineNumber":398,"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\terr = decoder(data, out)\n\t}()\n\n\tif recovered != nil {\n\t\treturn sharedStateCodecPanicError(\"decode\", format, recovered)\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"fiber: failed to decode shared state %s value: %w\", format, err)\n\t}\n\n\treturn nil\n}\n\nfunc sharedStateCodecNotConfiguredError(format, direction string) error {\n\treturn fmt.Errorf(\"fiber: shared state %s %s is not configured\", format, direction)\n}\n\nfunc sharedStateCodecPanicError(operation, format string, recovered any) error {\n\tif err, ok := recovered.(error); ok {\n\t\treturn fmt.Errorf(\"fiber: failed to %s shared state %s value: %w\", operation, format, err)\n\t}\n\n\treturn fmt.Errorf(\"fiber: failed to %s shared state %s value: %v\", operation, format, recovered)\n}\n\nfunc (s *SharedState) storageKey(key string) (string, bool) {","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/shared_state.go#L380-L416","documentation":"Returned by decodeSharedStateValue (shared_state.go:398) when a configured decoder (JSON/XML/MsgPack/CBOR) returns a non-nil error while deserializing stored bytes into the caller's output value during GetJSON/GetXML/GetMsgPack/GetCBOR. The format and original error are wrapped via %w.","triggerScenarios":"Calling GetJSON/GetXML/etc. where the stored bytes are malformed for the format, the target type does not match what was stored (schema drift), or a nil/non-pointer out argument causes the decoder to fail. The decoder error from shared_state.go:391 is captured.","commonSituations":"Stored payload written by a different encoder version (schema change), corrupted storage, reading JSON into a struct whose fields changed, passing a non-pointer or nil pointer as `out`, or a custom decoder rejecting the payload.","solutions":["Pass a non-nil pointer to the target type as the `out` argument.","Verify the stored bytes match the expected format — dump and inspect them.","Align struct tags / types between writer and reader; migrate stored data on schema changes.","If using a custom decoder, test it against the raw bytes in isolation."],"exampleFix":"// before: schema mismatch + non-pointer\nvar v OldShape\n_, _, err := state.GetJSON(ctx, \"k\", v)\n\n// after: matching schema, pointer target\nvar v CurrentShape\n_, ok, err := state.GetJSON(ctx, \"k\", &v)\nif err != nil { return err }\nif !ok { /* not stored */ }","handlingStrategy":"validation","validationCode":"var out Target\nif rv := reflect.ValueOf(out); rv.Kind() != reflect.Ptr || rv.IsNil() {\n    return errors.New(\"out must be a non-nil pointer\")\n}\n_, _, err := state.GetJSON(ctx, \"k\", &out)","typeGuard":"func isNonNilPointer(v any) bool {\n    rv := reflect.ValueOf(v)\n    return rv.Kind() == reflect.Ptr && !rv.IsNil()\n}","tryCatchPattern":"if _, _, err := state.GetJSON(ctx, \"k\", &out); err != nil {\n    if strings.Contains(err.Error(), \"failed to decode\") {\n        // schema mismatch or corrupt payload — migrate or re-store\n    }\n}","preventionTips":["Always pass a non-nil pointer as the out argument.","Keep reader/writer struct schemas in sync; migrate stored data on changes.","Dump and validate stored bytes when decode errors appear."],"tags":["shared-state","deserialization","decode","json","xml","msgpack","cbor","schema"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}