micro/go-micro · error
ErrInvalidMessage
ErrInvalidMessage
Error message
invalid message
What it means
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.
Source
Thrown at codec/codec.go:17
// Package codec is an interface for encoding messages
package codec
import (
"errors"
"io"
)
const (
Error MessageType = iota
Request
Response
Event
)
var (
ErrInvalidMessage = errors.New("invalid message")
)
type MessageType int
// Takes in a connection/buffer and returns a new Codec.
type NewCodec func(io.ReadWriteCloser) Codec
// Codec encodes/decodes various types of messages used within go-micro.
// ReadHeader and ReadBody are called in pairs to read requests/responses
// from the connection. Close is called when finished with the
// connection. ReadBody may be called with a nil argument to force the
// body to be read and discarded.
type Codec interface {
Reader
Writer
Close() error
String() string
}View on GitHub (pinned to 24529f1404)
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
Example fix
// before
msg := rpc.Message{Id: id}
err := codec.Write(&m, msg) // Type unset -> invalid message
// after
msg := rpc.Message{Id: id, Type: codec.Request}
err := codec.Write(&m, msg) Defensive patterns
Strategy: validation
Validate before calling
if msg.Type != codec.Request && msg.Type != codec.Response && msg.Type != codec.Event {
return fmt.Errorf("refusing to write message with invalid type %v", msg.Type)
} Type guard
func isValidMessageType(t codec.MessageType) bool {
return t == codec.Request || t == codec.Response || t == codec.Event
} Try / catch
if err := codec.Write(m, msg); errors.Is(err, codec.ErrInvalidMessage) {
return fmt.Errorf("message type %v unsupported by codec: %w", msg.Type, err)
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- unknown request path
- unsupported Content-Type
- failed to marshal: %v is not type of *bytes.Frame or proto.M
- failed to unmarshal: %v is not type of proto.Message
- failed to marshal: %v is not type of *[]byte
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/dffe7d9c3a0c877a.
Report an issue: GitHub.