{"id":"4ca824aa3fbf868a","repo":"gofiber/fiber","slug":"failed-to-encode-data-w","errorCode":null,"errorMessage":"failed to encode data: %w","messagePattern":"failed to encode data: %w","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"middleware/session/session.go","lineNumber":399,"sourceCode":"\t}\n\n\ts.mu.Lock()\n\tdefer s.mu.Unlock()\n\n\t// Set idleTimeout if not already set\n\tif s.idleTimeout <= 0 {\n\t\ts.idleTimeout = s.config.IdleTimeout\n\t}\n\n\t// Update client cookie\n\ts.setSession()\n\n\t// Encode session data\n\ts.data.RLock()\n\tencodedBytes, err := s.encodeSessionData()\n\ts.data.RUnlock()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to encode data: %w\", err)\n\t}\n\n\t// Pass copied bytes with session id to provider\n\treturn s.config.Storage.SetWithContext(ctx, s.id, encodedBytes, s.idleTimeout)\n}\n\n// Keys retrieves all keys in the current session.\n//\n// Returns:\n//   - []any: A slice of all keys in the session.\n//\n// Usage:\n//\n//\tkeys := s.Keys()\nfunc (s *Session) Keys() []any {\n\tif s.data == nil {\n\t\treturn []any{}\n\t}","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/session/session.go#L381-L417","documentation":"Returned by Session.saveSessionWithContext when encodeSessionData fails. Session data is serialized with encoding/gob; this error wraps a gob encoder failure while persisting the session on Save/SaveWithContext (or the automatic save at handler return).","triggerScenarios":"encCache.Encode(&s.data.Data) fails at session.go:609. gob fails when a value's type was not registered (for interface fields), when a type is not gob-encodable (e.g. channels, funcs, certain maps), or on cyclic structures.","commonSituations":"Storing a custom struct in the session without gob.Register; storing a map/struct containing non-encodable fields; storing an interface value whose concrete type gob doesn't know; storing time.Time or nested types that need registration.","solutions":["Register all custom concrete types you store in the session: gob.Register(MyType{}) in init().","Avoid storing funcs, channels, or unexported-field structs in session data.","Prefer primitive types (strings, ints, []byte) in session values.","Add a unit test that calls Save to surface encoding errors early."],"exampleFix":"// before: storing an unregistered custom type\ntype Cart struct{ Items []string }\nsess.Set(\"cart\", Cart{Items: []string{\"a\"}})\nsess.Save() // gob encode fails\n\n// after: register the type once at startup\nfunc init() {\n    gob.Register(Cart{})\n}","handlingStrategy":"validation","validationCode":"// Register every custom type stored in the session at startup.\nfunc init() {\n    gob.Register(MyType{})\n    gob.Register(map[string]any{})\n    gob.Register([]string{})\n}","typeGuard":"// isGobEncodable approximates which values gob can encode; use it to\n// filter session values before Set.\nfunc isGobEncodable(v any) bool {\n    switch v.(type) {\n    case chan struct{}, func():\n        return false\n    }\n    return true\n}","tryCatchPattern":"if err := sess.Save(); err != nil {\n    if strings.Contains(err.Error(), \"failed to encode data\") {\n        log.Error().Err(err).Msg(\"session gob encode failed\")\n        // keep the request alive without persisting the unencodable value\n        return c.SendStatus(fiber.StatusAccepted)\n    }\n    return err\n}","preventionTips":["gob.Register every concrete type stored in sessions during init().","Avoid funcs, channels, and unexported-field structs in session values.","Prefer primitives and []byte in session data.","Add a Save() smoke test for each session value type you use."],"tags":["session","gob","serialization","encoding","configuration"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}