{"record":{"id":"83fe7096de1cc1fa","repo":"micro/go-micro","slug":"error-mashaling-event-to-json","errorCode":null,"errorMessage":"Error mashaling event to JSON","messagePattern":"Error mashaling event to JSON","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"events/store.go","lineNumber":93,"sourceCode":"\t}\n\n\treturn result, nil\n}\n\n// Write an event to the store\nfunc (s *evStore) Write(event *Event, opts ...WriteOption) error {\n\t// parse the options\n\toptions := WriteOptions{\n\t\tTTL: s.opts.TTL,\n\t}\n\tfor _, o := range opts {\n\t\to(&options)\n\t}\n\n\t// construct the store record\n\tbytes, err := json.Marshal(event)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Error mashaling event to JSON\")\n\t}\n\t// suffix event ID with hour resolution for easy retrieval in batches\n\ttimeSuffix := time.Now().Format(\"2006010215\")\n\n\trecord := &store.Record{\n\t\t// key is such that reading by prefix indexes by topic and reading by suffix indexes by time\n\t\tKey:    event.Topic + joinKey + event.ID + joinKey + timeSuffix,\n\t\tValue:  bytes,\n\t\tExpiry: options.TTL,\n\t}\n\n\t// write the record to the store\n\tif err := s.store.Write(record); err != nil {\n\t\treturn errors.Wrap(err, \"Error writing to the store\")\n\t}\n\n\treturn nil\n}","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/events/store.go#L75-L111","documentation":"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.)","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nevt := &events.Event{Payload: map[string]interface{}{\"cb\": func(){}}} // marshal fails\n// after\nevt := &events.Event{Payload: map[string]interface{}{\"cbName\": \"handleFoo\"}}","handlingStrategy":"validation","validationCode":"func canMarshal(v interface{}) error {\n    _, err := json.Marshal(v)\n    return err\n}\n// call before Write:\nif err := canMarshal(event.Payload); err != nil { return err }","typeGuard":null,"tryCatchPattern":"err := evStore.Write(event)\nif err != nil {\n    if strings.Contains(err.Error(), \"mashaling event to JSON\") {\n        log.Printf(\"event payload not JSON-serializable: %v\", errors.Cause(err))\n        return nil // or sanitize and retry\n    }\n    return err\n}","preventionTips":["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"],"tags":["json","marshal","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}