geektutu/7days-golang · error

invalid codec type %s

Error message

invalid codec type %s

What it means

day5-http-debug's NewClient rejects an unregistered opt.CodecType with "invalid codec type %s". It is also invoked by NewHTTPClient, so HTTP-based connections hit the same codec-lookup rule as plain TCP dials.

Source

Thrown at gee-rpc/day5-http-debug/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

  1. Use codec.GobType (the default) for CodecType
  2. Register the wanted codec in codec.NewCodecFuncMap before connecting
  3. Pass no Option (or nil) to use DefaultOption
  4. Keep client and server codec constants in a shared package to avoid string drift

Example fix

// before
opt := &geerpc.Option{CodecType: "protobuf"} // never registered
// after
opt := &geerpc.Option{CodecType: codec.GobType}
Defensive patterns

Strategy: validation

Validate before calling

if _, ok := codec.NewCodecFuncMap[opt.CodecType]; opt.CodecType != "" && !ok {
	return fmt.Errorf("codec %q not registered", opt.CodecType)
}
client, err := geerpc.DialHTTP("tcp", addr, opt)

Type guard

func codecSupported(t string) bool {
	_, ok := codec.NewCodecFuncMap[t]
	return ok
}

Try / catch

client, err := geerpc.NewHTTPClient(conn, opt)
if err != nil && strings.Contains(err.Error(), "invalid codec type") {
	opt.CodecType = codec.GobType
	client, err = geerpc.NewHTTPClient(conn, opt)
}

Prevention

When it happens

Trigger: Dial or NewHTTPClient with an Option whose CodecType string is not a key in codec.NewCodecFuncMap (empty string is defaulted to Gob by parseOptions, any other unknown string fails).

Common situations: Hardcoding codec names instead of using codec package constants; assuming JSON/protobuf support exists when only Gob is registered; client binary missing the codec registration the server uses.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/83cc741ff45be740. Report an issue: GitHub.