gofiber/fiber · error

failed to encode session data

Error message

failed to encode session data: %w

What it means

Thrown by the low-level encodeSessionData when gob.Encoder.Encode rejects &s.data.Data. This is the inner error that error 200 wraps; seeing it directly means encodeSessionData was called outside the save path or the wrapping was stripped. The %w is the raw gob encoder error naming the offending type or value.

Solutions

  1. Inspect the wrapped gob error for the offending type name.
  2. gob.Register every custom type stored in the session, in an init().
  3. Only store gob-encodable values (primitives, slices, maps, structs of encodable fields).

Example fix

// before
sess.Set("payload", MyStruct{Cb: func(){}})
// encodeSessionData -> failed to encode session data: gob: unsupported type

// after
func init() { gob.Register(MyStruct{}) }
// and remove non-encodable fields (func/chan) from the stored type
Defensive patterns

Strategy: validation

Validate before calling

func registerSessionTypes() {
    gob.Register(MyStruct{})
    gob.Register(&MyStruct{})
}
func encodableKindTest(v any) error {
    if v == nil { return nil }
    if k := reflect.TypeOf(v).Kind(); k == reflect.Chan || k == reflect.Func ||
        k == reflect.Complex64 || k == reflect.Complex128 {
        return fmt.Errorf("unsupported kind %s", k)
    }
    return nil
}

Type guard

func isGobSafe(v any) bool {
    if v == nil { return true }
    switch reflect.TypeOf(v).Kind() {
    case reflect.Chan, reflect.Func, reflect.Complex64, reflect.Complex128:
        return false
    }
    return true
}

Try / catch

if _, err := sess.encodeSessionDataProbe(); err != nil {
    log.Printf("encode failed, dropping offending keys: %v", err)
}

Prevention

When it happens

Trigger: Same as 200: s.data.Data contains a value gob cannot encode — channels, funcs, complex numbers, unregistered interface concrete types, structs holding any of these, or cyclic structures.

Common situations: Storing custom struct types without gob.Register; storing closures; dynamic map[string]any values whose concrete types gob does not know about.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/4484f007876b2f63. Report an issue: GitHub.

Appendix: source

Thrown at middleware/session/session.go:610

// encodeSessionData encodes session data to raw bytes
//
// Parameters:
//   - rawData: The raw byte data to encode.
//
// Returns:
//   - error: An error if the encoding fails.
//
// Usage:
//
//	err := s.encodeSessionData(rawData)
func (s *Session) encodeSessionData() ([]byte, error) {
	byteBuffer := byteBufferPool.Get().(*bytes.Buffer) //nolint:forcetypeassert,errcheck // We store nothing else in the pool
	defer byteBufferPool.Put(byteBuffer)
	defer byteBuffer.Reset()
	encCache := gob.NewEncoder(byteBuffer)
	if err := encCache.Encode(&s.data.Data); err != nil {
		return nil, fmt.Errorf("failed to encode session data: %w", err)
	}
	// Copy the bytes
	// Copy the data in buffer
	encodedBytes := make([]byte, byteBuffer.Len())
	copy(encodedBytes, byteBuffer.Bytes())

	return encodedBytes, nil
}

// absExpiration returns the session absolute expiration time or a zero time if not set.
//
// Returns:
//   - time.Time: The session absolute expiration time. Zero time if not set.
//
// Usage:
//
//	expiration := s.absExpiration()
func (s *Session) absExpiration() time.Time {

View on GitHub (pinned to a105acad6c)