grpc/grpc-go · critical
cannot register Codec with empty string result for Name()
Error message
cannot register Codec with empty string result for Name()
What it means
encoding.RegisterCodec (encoding.go:132) panics if codec.Name() returns an empty string (checked at encoding.go:137). The Name() result is the content-subtype used as the map key and in the Content-Type header (e.g. 'proto', 'json'); an empty name would be unaddressable and break codec lookup.
Source
Thrown at encoding/encoding.go:137
// servers.
//
// The Codec will be stored and looked up by result of its Name() method, which
// should match the content-subtype of the encoding handled by the Codec. This
// is case-insensitive, and is stored and looked up as lowercase. If the
// result of calling Name() is an empty string, RegisterCodec will panic. See
// Content-Type on
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for
// more details.
//
// NOTE: this function must only be called during initialization time (i.e. in
// an init() function), and is not thread-safe. If multiple Codecs are
// registered with the same name, the one registered last will take effect.
func RegisterCodec(codec Codec) {
if codec == nil {
panic("cannot register a nil Codec")
}
if codec.Name() == "" {
panic("cannot register Codec with empty string result for Name()")
}
contentSubtype := strings.ToLower(codec.Name())
registeredCodecs[contentSubtype] = codec
}
// GetCodec gets a registered Codec by content-subtype, or nil if no Codec is
// registered for the content-subtype.
//
// The content-subtype is expected to be lowercase.
func GetCodec(contentSubtype string) Codec {
c, _ := registeredCodecs[contentSubtype].(Codec)
return c
}
View on GitHub (pinned to 03255a9237)
Solutions
- Ensure the codec's Name() returns a non-empty, content-subtype-valid string (e.g. "json").
- If the name comes from config, validate it is non-empty before registering.
- Add a unit test asserting codec.Name() != "" before calling RegisterCodec.
Example fix
// before
type myCodec struct{ name string }
func (m *myCodec) Name() string { return m.name } // empty when unset
func init() { encoding.RegisterCodec(&myCodec{}) } // panics
// after
func init() { encoding.RegisterCodec(&myCodec{name: "myjson"}) } Defensive patterns
Strategy: validation
Validate before calling
func init() {
c := buildCodec()
if c.Name() == "" {
log.Fatal("codec Name() is empty")
}
encoding.RegisterCodec(c)
} Prevention
- Implement Name() to return a literal non-empty content-subtype.
- Validate any config-sourced name before registering.
- Add a test asserting codec.Name() is non-empty.
When it happens
Trigger: Registering a Codec whose Name() method returns "" (e.g. a struct whose name field was never set, or a Name() implementation that returns a zero-value string).
Common situations: A codec built from config where the name field defaulted to empty; copy-pasting a codec template and forgetting to implement Name() to return a real value; a codec whose name is computed from an uninitialized variable.
Related errors
- cannot register CodecV2 with empty string result for Name()
- cannot register a nil Codec
- cannot register a nil CodecV2
- grpc: invalid gzip compression level: %d
- metadata: Pairs got the odd number of input pairs for metada
AI-assisted analysis of grpc/grpc-go@03255a9237 (2026-08-07).
Data as JSON: /api/errors/b5e8f11a0031ba1e.
Report an issue: GitHub.