geektutu/7days-golang · error

invalid codec type %s

Error message

invalid codec type %s

What it means

NewClient in gee-rpc/day3-service rejects an Option whose CodecType has no registered codec constructor, returning "invalid codec type %s". The client cannot negotiate a message codec with the server, so it aborts before sending options.

Source

Thrown at gee-rpc/day3-service/client.go:205

	// 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)
		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 for opt.CodecType or leave it empty (defaults to Gob)
  2. Register the desired codec in codec.NewCodecFuncMap before dialing, identically on client and server
  3. Pass nil opts to Dial to accept DefaultOption
  4. Cross-check the exact string constants in the codec package rather than hardcoding literals

Example fix

// before
opt := &geerpc.Option{CodecType: "application/json"} // not registered
// after
import "geerpc/codec"
opt := &geerpc.Option{CodecType: codec.GobType}
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

client, err := geerpc.Dial("tcp", addr, opt)
if err != nil && strings.Contains(err.Error(), "invalid codec type") {
	opt.CodecType = "" // default to Gob via parseOptions
	client, err = geerpc.Dial("tcp", addr, opt)
}

Prevention

When it happens

Trigger: Dial/DialHTTP-style connection via NewClient with opt.CodecType set to an unregistered string (e.g. "application/json" when only GobType is in NewCodecFuncMap).

Common situations: Client and server configured with different codec strings; hand-written Option with a typo; expecting JSON support that is not registered by default; custom codec registered only on one side.

Related errors


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