{"record":{"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/a105acad6c1e4576a77f01e02973f67e962bb58d/shared_state.go#L350-L386","documentation":"Returned by encodeSharedStateValue() when the configured encoder (json/msgpack/cbor/xml) returns a non-nil error while serializing a value for SharedState.SetJSON/SetMsgPack/SetCBOR/SetXML. Fiber also recovers panics from the codec separately (sharedStateCodecPanicError); this error is the non-panic failure path.","triggerScenarios":"Calling app.State().Shared().SetJSON(key, v, ttl) (or MsgPack/CBOR/XML variants) where v cannot be marshaled: a channel, func, or cyclic reference for JSON; an XML-incompatible type (map with non-string keys); a type whose MarshalBinary/MarshalJSON returns an error.","commonSituations":"Storing a struct that embeds an unexported type the codec refuses; switching a value type after deployment to one the codec cannot serialize; a custom MarshalJSON that errors on a nil receiver; passing an io.Reader or chan field.","solutions":["Inspect the wrapped error — encoders name the offending field/type.","Add a MarshalJSON/MarshalBinary method or remove the unserializable field (chan, func, io.Reader).","For XML, ensure map keys are string-convertible or use a slice of structs.","Unit-test marshaling of every type you store in SharedState."],"exampleFix":"// before — struct with a chan field cannot be JSON-encoded\ntype Job struct{ ID string; Done chan struct{} }\napp.State().Shared().SetJSON(\"job:1\", Job{ID: \"1\", Done: make(chan struct{})}, 0)\n// -> fiber: failed to encode shared state json value: json: unsupported type: chan struct {}\n\n// after — drop the unserializable field or provide a custom marshaler\ntype Job struct{ ID string }\napp.State().Shared().SetJSON(\"job:1\", Job{ID: \"1\"}, 0)","handlingStrategy":"validation","validationCode":"// Encode a representative value at startup to fail fast.\nfunc validateEncodable(enc func(any) ([]byte, error), sample any) error {\n    if _, err := enc(sample); err != nil {\n        return fmt.Errorf(\"value not encodable: %w\", err)\n    }\n    return nil\n}","typeGuard":"// Reject types known to be unserializable before storing.\nfunc encodableForJSON(v any) bool {\n    rv := reflect.ValueOf(v)\n    switch rv.Kind() {\n    case reflect.Chan, reflect.Func, reflect.Invalid:\n        return false\n    }\n    return true\n}","tryCatchPattern":"if err := app.State().Shared().SetJSON(k, v, ttl); err != nil {\n    log.Printf(\"shared state encode failed for %s: %v\", k, err)\n    return fiber.ErrInternalServerError\n}","preventionTips":["Unit-test marshaling of every stored type.","Drop chan/func/io.Reader fields or add a custom marshaler.","For XML, ensure map keys are string-convertible."],"tags":["shared-state","serialization","json","msgpack","cbor","xml","encoding"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}