geektutu/7days-golang · error
invalid codec type %s
Error message
invalid codec type %s
What it means
Day7-registry variant of the codec lookup failure: NewClient cannot find a constructor for opt.CodecType in codec.NewCodecFuncMap and returns this error after logging it. Guarantees the client never proceeds with an unknown serialization protocol.
Source
Thrown at gee-rpc/day7-registry/client.go:216
// if opts is nil or pass nil as parameter
if len(opts) == 0 || opts[0] == nil {
return DefaultOption, nil
}
if len(opts) != 1 {
return nil, errors.New("number of options is more than 1")
}
opt := opts[0]
opt.MagicNumber = DefaultOption.MagicNumber
if opt.CodecType == "" {
opt.CodecType = DefaultOption.CodecType
}
return opt, nil
}
func NewClient(conn net.Conn, opt *Option) (*Client, error) {
f := codec.NewCodecFuncMap[opt.CodecType]
if f == nil {
err := fmt.Errorf("invalid codec type %s", opt.CodecType)
log.Println("rpc client: codec error:", err)
return nil, err
}
// send options with server
if err := json.NewEncoder(conn).Encode(opt); err != nil {
log.Println("rpc client: options error: ", err)
_ = conn.Close()
return nil, err
}
return newClientCodec(f(conn), opt), nil
}
func newClientCodec(cc codec.Codec, opt *Option) *Client {
client := &Client{
seq: 1, // seq starts with 1, 0 means invalid call
cc: cc,
opt: opt,
pending: make(map[uint64]*Call),View on GitHub (pinned to cf36443821)
Solutions
- Use geerpc.DefaultOption (CodecType 'gee-gob') or set CodecType to a registered codec name.
- Import the codec implementation package so its init() registers the codec function.
- Ensure the server accepts the same CodecType.
Example fix
// before
opt := &geerpc.Option{ConnectTimeout: time.Second} // CodecType missing
// after
opt := geerpc.DefaultOption // CodecType: "gee-gob" Defensive patterns
Strategy: validation
Validate before calling
if _, ok := codec.NewCodecFuncMap[opt.CodecType]; !ok {
opt = geerpc.DefaultOption // or set a registered CodecType
} Try / catch
client, err := geerpc.NewClient(conn, opt)
if err != nil {
if strings.Contains(err.Error(), "invalid codec type") {
// retry once with DefaultOption
client, err = geerpc.NewClient(conn, geerpc.DefaultOption)
}
if err != nil { return err }
} Prevention
- Derive Options from DefaultOption rather than zero-value structs.
- Blank-import the codec package so registration init runs.
- Use shared constants for codec names across services.
- Check server logs — the server performs the same codec lookup.
When it happens
Trigger: NewClient/NewHTTPClient called with Option.CodecType empty or set to a name never registered via codec.NewCodecFuncMap (e.g. 'gob'), including manually built Options bypassing DefaultOption.
Common situations: Custom Option structs missing CodecType; importing client code without the codec package's registration init; client/server using different codec name strings after a rename.
Related errors
- invalid codec type %s
- invalid codec type %s
- invalid codec type %s
- invalid codec type %s
- invalid codec type %s
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/d944b0ba26ce0e49.
Report an issue: GitHub.