{"record":{"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/a105acad6c1e4576a77f01e02973f67e962bb58d/shared_state.go#L380-L416","documentation":"Returned by decodeSharedStateValue() when the configured decoder (json/msgpack/cbor/xml) returns a non-nil error while deserializing stored bytes into the output value during SharedState.GetJSON/GetMsgPack/GetCBOR/GetXML. Panics from the codec are handled separately (sharedStateCodecPanicError).","triggerScenarios":"Calling app.State().Shared().GetJSON(key, &out) where the stored bytes are corrupt, truncated, or were encoded with a different codec than the one used to decode (e.g., stored as msgpack, read with json). Also fires on type mismatch — decoding into a struct whose fields do not match the stored payload.","commonSituations":"Codec migration: data written with JSON is read with MsgPack; schema drift after a struct field rename; partial write left truncated bytes in storage; storage backend returned corrupted data (Redis eviction mid-write); wrong key namespace returning another app's data.","solutions":["Verify the encode and decode codecs match for a given key namespace.","Check the wrapped error for syntax/EOF/type messages and reconcile the target struct.","If migrating codecs, migrate the stored data or version the key prefix.","Confirm storage integrity (no truncation) — re-Set the value from a known-good source."],"exampleFix":"// before — wrote msgpack, read json\napp.State().Shared().SetMsgPack(\"u:1\", u, 0)\napp.State().Shared().GetJSON(\"u:1\", &u2)\n// -> fiber: failed to decode shared state json value: invalid character ...\n\n// after — match codecs\napp.State().Shared().SetMsgPack(\"u:1\", u, 0)\napp.State().Shared().GetMsgPack(\"u:1\", &u2)","handlingStrategy":"try-catch","validationCode":"// Version keys by codec to prevent cross-codec reads.\nfunc keyFor(codec, key string) string { return codec + \":\" + key }\n// On decode failure, fall back to a safe default rather than crashing.\nfunc getOrDefaultJSON(app *fiber.App, key string, out any, def any) {\n    _, ok, err := app.State().Shared().GetJSON(key, out)\n    if err != nil || !ok { out = def }\n}","typeGuard":null,"tryCatchPattern":"_, ok, err := app.State().Shared().GetJSON(k, &out)\nif err != nil {\n    log.Printf(\"shared state decode failed for %s: %v\", k, err)\n    // optionally treat as cache miss and rebuild\n    out = freshValue()\n} else if !ok {\n    out = freshValue()\n}","preventionTips":["Use one codec per key namespace; namespace keys by codec when migrating.","Reconcile struct field types with stored payloads after schema changes.","Validate storage integrity after disruptive deployments."],"tags":["shared-state","deserialization","json","msgpack","cbor","xml","decoding"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}