micro/go-micro · error
unrecognized message type: %v
Error message
unrecognized message type: %v
What it means
The jsonrpc codec's Write method switches on the codec.MessageType (Request, Response, etc.) to decide how to serialize the message. Any message type it does not explicitly handle falls through to default and yields this error, meaning the codec cannot encode the given message kind.
Source
Thrown at codec/jsonrpc/jsonrpc.go:44
func (j *jsonCodec) String() string {
return "json-rpc"
}
func (j *jsonCodec) Write(m *codec.Message, b interface{}) error {
switch m.Type {
case codec.Request:
return j.c.Write(m, b)
case codec.Response, codec.Error:
return j.s.Write(m, b)
case codec.Event:
data, err := json.Marshal(b)
if err != nil {
return err
}
_, err = j.rwc.Write(data)
return err
default:
return fmt.Errorf("unrecognized message type: %v", m.Type)
}
}
func (j *jsonCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
j.buf.Reset()
j.mt = mt
switch mt {
case codec.Request:
return j.s.ReadHeader(m)
case codec.Response:
return j.c.ReadHeader(m)
case codec.Event:
_, err := io.Copy(j.buf, j.rwc)
return err
default:
return fmt.Errorf("unrecognized message type: %v", mt)
}View on GitHub (pinned to 24529f1404)
Solutions
- Use a message Type supported by the jsonrpc codec (Request/Response as implemented).
- Switch to the appropriate codec for that message type (e.g. bytes or grpc codec supports more flows).
- Verify the client/server configuration is not routing Event/Publish traffic through the jsonrpc codec.
- Check that codec.Message.Type is set explicitly, not left as zero value.
Example fix
// before msg.Type = codec.Event jcodec.Write(msg, body) // after msg.Type = codec.Request jcodec.Write(msg, body)
Defensive patterns
Strategy: type-guard
Validate before calling
func jsonrpcSupports(mt codec.MessageType) bool {
return mt == codec.Request || mt == codec.Response || mt == codec.Event
} Type guard
func isJsonrpcWritable(m *codec.Message) bool {
return m != nil && jsonrpcSupports(m.Type)
} Try / catch
if err := c.Write(msg, body); err != nil {
if strings.HasPrefix(err.Error(), "unrecognized message type") {
// unsupported MessageType for this codec; use another codec or fix msg.Type
}
return err
} Prevention
- Set codec.Message.Type explicitly and to a supported value.
- Do not route Event/publish traffic through codecs that do not implement it.
- Prefer built-in transports that assign message types for you.
- Add a switch-level test enumerating every MessageType your flow uses.
When it happens
Trigger: Calling Write on a jsonrpc codec with a message whose Type is not one of the handled types (e.g. codec.Event if unhandled, or a zero-value MessageType).
Common situations: Custom transport code sending raw codec.Message values with a hand-set Type; framework internals publishing events through a client configured with the jsonrpc codec that does not support that message type; constructing codec.Message manually in tests.
Related errors
- invalid error %v
- ErrInvalidMessage
- unknown request path
- unsupported Content-Type
- failed to marshal: %v is not type of *bytes.Frame or proto.M
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/a626f810cb01d453.
Report an issue: GitHub.