Troubleshooting
This guide covers common issues and solutions when using Fory Go.
Error Types
Fory Go uses typed errors with specific error kinds:
type Error struct {
kind ErrorKind
message string
// Additional context fields
}
func (e Error) Kind() ErrorKind { return e.kind }
func (e Error) Error() string { return e.message }
Error Kinds
| Kind | Value | Description |
|---|---|---|
ErrKindOK | 0 | No error |
ErrKindBufferOutOfBound | 1 | Read/write beyond buffer bounds |
ErrKindTypeMismatch | 2 | Type ID mismatch |
ErrKindUnknownType | 3 | Unknown type encountered |
ErrKindSerializationFailed | 4 | General serialization failure |
ErrKindDeserializationFailed | 5 | General deserialization failure |
ErrKindMaxDepthExceeded | 6 | Recursion depth limit exceeded |
ErrKindNilPointer | 7 | Unexpected nil pointer |
ErrKindInvalidRefId | 8 | Invalid reference ID |
ErrKindHashMismatch | 9 | Struct hash mismatch |
ErrKindInvalidTag | 10 | Invalid fory struct tag |
Common Errors and Solutions
ErrKindUnknownType
Error: unknown type encountered
Cause: Type not registered before serialization/deserialization.
Solution:
f := fory.New()
// Register type before use
f.RegisterStruct(User{}, 1)
// Now serialization works
data, _ := f.Serialize(&User{ID: 1})
ErrKindTypeMismatch
Error: type mismatch: expected X, got Y
Cause: Serialized data has different type than expected.
Solutions:
- Use correct target type:
// Wrong: Deserializing User into Order
var order Order
f.Deserialize(userData, &order) // Error!
// Correct
var user User
f.Deserialize(userData, &user)
- Ensure consistent registration:
// Serializer
f1 := fory.New()
f1.RegisterStruct(User{}, 1)
// Deserializer - must use same ID
f2 := fory.New()
f2.RegisterStruct(User{}, 1) // Same ID!
ErrKindHashMismatch
Error: hash X is not consistent with Y for type Z
Cause: Struct definition changed between serialization and deserialization.
Solutions:
- Enable compatible mode:
f := fory.New(fory.WithCompatible(true))
- Ensure struct definitions match:
// Both serializer and deserializer must have same struct
type User struct {
ID int64
Name string
}
- Regenerate codegen (if using):
go generate ./...
ErrKindMaxDepthExceeded
Error: max depth exceeded
Cause: Data nesting exceeds maximum depth limit.
Possible causes:
- Deeply nested data structures exceeding the default limit (20)
- Unintended circular references without reference tracking enabled
- Malicious data: Attackers may craft deeply nested payloads to cause resource exhaustion
Solutions:
- Increase max depth (default is 20):
f := fory.New(fory.WithMaxDepth(50))
- Enable reference tracking (for circular data):
f := fory.New(fory.WithTrackRef(true))
-
Check for unintended circular references in your data.
-
Validate untrusted data: When deserializing data from untrusted sources, do not blindly increase max depth. Consider validating input size and structure before deserialization.
ErrKindBufferOutOfBound
Error: buffer out of bound: offset=X, need=Y, size=Z
Cause: Reading beyond available data.
Solutions:
- Ensure complete data transfer:
// Wrong: Truncated data
data := fullData[:100]
f.Deserialize(data, &target) // Error if data was larger
// Correct: Use full data
f.Deserialize(fullData, &target)