micro/go-micro · error
Error mashaling event to JSON
Error message
Error mashaling event to JSON
What it means
evStore.Write marshals the Event to JSON before storing it. This error wraps a json.Marshal failure, meaning the event could not be serialized — typically because it contains unsupported types (channels, funcs, cycles) in its payload. (The message's 'mashaling' typo comes from the library.)
Source
Thrown at events/store.go:93
}
return result, nil
}
// Write an event to the store
func (s *evStore) Write(event *Event, opts ...WriteOption) error {
// parse the options
options := WriteOptions{
TTL: s.opts.TTL,
}
for _, o := range opts {
o(&options)
}
// construct the store record
bytes, err := json.Marshal(event)
if err != nil {
return errors.Wrap(err, "Error mashaling event to JSON")
}
// suffix event ID with hour resolution for easy retrieval in batches
timeSuffix := time.Now().Format("2006010215")
record := &store.Record{
// key is such that reading by prefix indexes by topic and reading by suffix indexes by time
Key: event.Topic + joinKey + event.ID + joinKey + timeSuffix,
Value: bytes,
Expiry: options.TTL,
}
// write the record to the store
if err := s.store.Write(record); err != nil {
return errors.Wrap(err, "Error writing to the store")
}
return nil
}View on GitHub (pinned to 24529f1404)
Solutions
- Inspect errors.Cause(err) — json.Marshal names the unsupported type/field
- Remove or replace unsupported fields (chan, func, cycles) in the event payload
- Implement json.Marshaler on custom types carried in the event
- Sanitize/normalize the payload before constructing the Event
Example fix
// before
evt := &events.Event{Payload: map[string]interface{}{"cb": func(){}}} // marshal fails
// after
evt := &events.Event{Payload: map[string]interface{}{"cbName": "handleFoo"}} Defensive patterns
Strategy: validation
Validate before calling
func canMarshal(v interface{}) error {
_, err := json.Marshal(v)
return err
}
// call before Write:
if err := canMarshal(event.Payload); err != nil { return err } Try / catch
err := evStore.Write(event)
if err != nil {
if strings.Contains(err.Error(), "mashaling event to JSON") {
log.Printf("event payload not JSON-serializable: %v", errors.Cause(err))
return nil // or sanitize and retry
}
return err
} Prevention
- Never put chan, func, or cyclic structures in Event payloads
- Implement json.Marshaler on custom payload types
- Unit-test marshaling of every event type you publish
- Sanitize external/user data before embedding it in events
When it happens
Trigger: Calling evStore.Write(event) with an Event whose Payload/Data contains values json.Marshal cannot encode: circular references, channels, function values, or invalid UTF-8 in strings.
Common situations: Passing a struct with a func or chan field; self-referential data structures; custom types without MarshalJSON that hold unsupported fields; embedding a context or mutex-bearing object in the event.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- failed to marshal request: %w
- failed to marshal stream request: %w
- failed to marshal request: %w
- failed to marshal stream request: %w
- failed to marshal request: %w
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/83fe7096de1cc1fa.
Report an issue: GitHub.