{"record":{"id":"dffe7d9c3a0c877a","repo":"micro/go-micro","slug":"errinvalidmessage","errorCode":"ErrInvalidMessage","errorMessage":"invalid message","messagePattern":"invalid message","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codec/codec.go","lineNumber":17,"sourceCode":"// Package codec is an interface for encoding messages\npackage codec\n\nimport (\n\t\"errors\"\n\t\"io\"\n)\n\nconst (\n\tError MessageType = iota\n\tRequest\n\tResponse\n\tEvent\n)\n\nvar (\n\tErrInvalidMessage = errors.New(\"invalid message\")\n)\n\ntype MessageType int\n\n// Takes in a connection/buffer and returns a new Codec.\ntype NewCodec func(io.ReadWriteCloser) Codec\n\n// Codec encodes/decodes various types of messages used within go-micro.\n// ReadHeader and ReadBody are called in pairs to read requests/responses\n// from the connection. Close is called when finished with the\n// connection. ReadBody may be called with a nil argument to force the\n// body to be read and discarded.\ntype Codec interface {\n\tReader\n\tWriter\n\tClose() error\n\tString() string\n}","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/codec/codec.go#L1-L35","documentation":"ErrInvalidMessage in codec/codec.go:17 is the sentinel error returned by codec operations (Marshal, Unmarshal, ReadBody, Write) when a message type is not one of the supported MessageType values (Request, Response, Event). The codec cannot determine how to encode/decode a message of unknown classification.","triggerScenarios":"Calling codec.Write/Marshal/Unmarshal/ReadBody with a MessageType outside the declared enum (e.g. zero value MessageType, or a custom/extended type the codec doesn't know).","commonSituations":"Constructing Messages manually without setting Type; custom middleware adding a new message type without extending the codec; version drift between client and server libraries using different message type sets.","solutions":["Ensure every message has Type explicitly set to codec.Request, codec.Response, or codec.Event before Write/Marshal","Check for zero-valued Message structs passed to the codec (Go zero value of MessageType is not valid here)","Align client and server library versions so message type enums match","Validate message construction in tests to catch unset Type fields early"],"exampleFix":"// before\nmsg := rpc.Message{Id: id}\nerr := codec.Write(&m, msg) // Type unset -> invalid message\n// after\nmsg := rpc.Message{Id: id, Type: codec.Request}\nerr := codec.Write(&m, msg)","handlingStrategy":"validation","validationCode":"if msg.Type != codec.Request && msg.Type != codec.Response && msg.Type != codec.Event {\n    return fmt.Errorf(\"refusing to write message with invalid type %v\", msg.Type)\n}","typeGuard":"func isValidMessageType(t codec.MessageType) bool {\n    return t == codec.Request || t == codec.Response || t == codec.Event\n}","tryCatchPattern":"if err := codec.Write(m, msg); errors.Is(err, codec.ErrInvalidMessage) {\n    return fmt.Errorf(\"message type %v unsupported by codec: %w\", msg.Type, err)\n}","preventionTips":["Always set Message.Type explicitly when constructing messages manually","Add constructor helpers that force a valid Type","Keep client/server library versions in sync so enums match"],"tags":["codec","serialization","message","micro","go"],"backgroundTag":"invalid-message-type","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}