{"record":{"id":"a1fde887f62a1352","repo":"micro/go-micro","slug":"errencodingmessage","errorCode":"ErrEncodingMessage","errorMessage":"error encoding message","messagePattern":"error encoding message","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"events/events.go","lineNumber":21,"sourceCode":"\nimport (\n\t\"encoding/json\"\n\t\"errors\"\n\t\"time\"\n)\n\nvar (\n\t// DefaultStream is the default events stream implementation\n\tDefaultStream Stream\n\t// DefaultStore is the default events store implementation\n\tDefaultStore Store\n)\n\nvar (\n\t// ErrMissingTopic is returned if a blank topic was provided to publish\n\tErrMissingTopic = errors.New(\"missing topic\")\n\t// ErrEncodingMessage is returned from publish if there was an error encoding the message option\n\tErrEncodingMessage = errors.New(\"error encoding message\")\n)\n\n// Stream is an event streaming interface\ntype Stream interface {\n\tPublish(topic string, msg interface{}, opts ...PublishOption) error\n\tConsume(topic string, opts ...ConsumeOption) (<-chan Event, error)\n}\n\n// Store is an event store interface\ntype Store interface {\n\tRead(topic string, opts ...ReadOption) ([]*Event, error)\n\tWrite(event *Event, opts ...WriteOption) error\n}\n\ntype AckFunc func() error\ntype NackFunc func() error\n\n// Event is the object returned by the broker when you subscribe to a topic","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/events/events.go#L3-L39","documentation":"ErrEncodingMessage is returned by events.Publish when the message option cannot be encoded into the event payload. The events layer marshals messages (e.g. to JSON) before publishing; an unsupported or unencodable value produces this sentinel error.","triggerScenarios":"Publishing a message the configured codec cannot marshal — e.g. a value containing channels, funcs, cyclic references, or a type whose MarshalJSON returns an error — when calling Stream.Publish(topic, msg, opts...).","commonSituations":"Publishing structs with unexported-only fields producing empty/invalid JSON; passing context.Context or other non-serializable values embedded in the payload; a custom marshaler returning an error (e.g. time.Time in an invalid state).","solutions":["Inspect the underlying error (errors.Is/Unwrap) to find which field failed to encode.","Ensure published messages are plain serializable data: exported fields, no channels/funcs/cycles; convert to a DTO struct if needed.","If using a custom MarshalJSON, fix or guard it so it never returns an error for valid domain states.","Log the message type and a sanitized snapshot when this sentinel occurs to identify the offending payload quickly."],"exampleFix":"// before\nstream.Publish(\"orders\", struct {\n    ctx context.Context\n}{} ) // unexported/non-serializable\n// after\ntype OrderEvent struct {\n    ID string `json:\"id\"`\n}\nstream.Publish(\"orders\", OrderEvent{ID: id})","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(msg); err != nil {\n    return fmt.Errorf(\"message not publishable: %w\", err)\n}","typeGuard":"func serializable(v interface{}) bool {\n    _, err := json.Marshal(v)\n    return err == nil\n}","tryCatchPattern":"if err := stream.Publish(topic, msg); err != nil {\n    if errors.Is(err, events.ErrEncodingMessage) {\n        return fmt.Errorf(\"publish: cannot encode %T: %w\", msg, err)\n    }\n    return err\n}","preventionTips":["Publish only DTO structs with exported, JSON-serializable fields","Keep channels, contexts, funcs, and cycles out of event payloads","Add a marshal smoke test for every event type in CI"],"tags":["events","pubsub","encoding","serialization"],"backgroundTag":"message-encoding-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}