{"record":{"id":"c9886f76b3363d80","repo":"geektutu/7days-golang","slug":"invalid-codec-type-s","errorCode":null,"errorMessage":"invalid codec type %s","messagePattern":"invalid codec type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-rpc/day2-client/client.go","lineNumber":205,"sourceCode":"\t// if opts is nil or pass nil as parameter\n\tif len(opts) == 0 || opts[0] == nil {\n\t\treturn DefaultOption, nil\n\t}\n\tif len(opts) != 1 {\n\t\treturn nil, errors.New(\"number of options is more than 1\")\n\t}\n\topt := opts[0]\n\topt.MagicNumber = DefaultOption.MagicNumber\n\tif opt.CodecType == \"\" {\n\t\topt.CodecType = DefaultOption.CodecType\n\t}\n\treturn opt, nil\n}\n\nfunc NewClient(conn net.Conn, opt *Option) (*Client, error) {\n\tf := codec.NewCodecFuncMap[opt.CodecType]\n\tif f == nil {\n\t\terr := fmt.Errorf(\"invalid codec type %s\", opt.CodecType)\n\t\tlog.Println(\"rpc client: codec error:\", err)\n\t\treturn nil, err\n\t}\n\t// send options with server\n\tif err := json.NewEncoder(conn).Encode(opt); err != nil {\n\t\tlog.Println(\"rpc client: options error: \", err)\n\t\t_ = conn.Close()\n\t\treturn nil, err\n\t}\n\treturn newClientCodec(f(conn), opt), nil\n}\n\nfunc newClientCodec(cc codec.Codec, opt *Option) *Client {\n\tclient := &Client{\n\t\tseq:     1, // seq starts with 1, 0 means invalid call\n\t\tcc:      cc,\n\t\topt:     opt,\n\t\tpending: make(map[uint64]*Call),","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-rpc/day2-client/client.go#L187-L223","documentation":"NewClient in gee-rpc/day2-client looks up the codec constructor in codec.NewCodecFuncMap by opt.CodecType; if the name is not registered it returns \"invalid codec type %s\" and refuses to connect. Only codecs registered in NewCodecFuncMap (e.g. Gob \"application/gob\") are supported.","triggerScenarios":"Dial(addr, opt) / NewClient(conn, opt) is called with an Option whose CodecType is a non-empty string that is not a registered key, e.g. opt.CodecType = \"json\" when no JSON codec was registered on the client side.","commonSituations":"Copy-pasting an Option with a made-up CodecType; server registers a custom codec type but the client build does not; typo like \"gob\" vs \"application/gob\"; forgetting that parseOptions only defaults CodecType when it is empty, not when it is wrong.","solutions":["Set opt.CodecType to a registered type such as codec.GobType, or leave it empty to use the default","Use geerpc.DefaultOption or omit the *Option argument to Dial","If a custom codec is needed, register it in codec.NewCodecFuncMap under the exact CodecType string on both client and server","Print codec.NewCodecFuncMap keys to confirm the exact accepted name"],"exampleFix":"// before\nopt := &geerpc.Option{CodecType: \"json\"}\nclient, err := geerpc.Dial(\"tcp\", \"localhost:9000\", opt)\n// after\nopt := &geerpc.Option{CodecType: codec.GobType} // \"application/gob\"\nclient, err := geerpc.Dial(\"tcp\", \"localhost:9000\", opt)","handlingStrategy":"validation","validationCode":"if _, ok := codec.NewCodecFuncMap[opt.CodecType]; !ok {\n\treturn fmt.Errorf(\"codec %q not registered; use codec.GobType\", opt.CodecType)\n}\nclient, err := geerpc.Dial(\"tcp\", addr, opt)","typeGuard":"func codecSupported(t string) bool {\n\t_, ok := codec.NewCodecFuncMap[t]\n\treturn ok\n}","tryCatchPattern":"client, err := geerpc.Dial(\"tcp\", addr, opt)\nif err != nil {\n\tif strings.Contains(err.Error(), \"invalid codec type\") {\n\t\topt.CodecType = codec.GobType // fall back to default codec\n\t\tclient, err = geerpc.Dial(\"tcp\", addr, opt)\n\t}\n\tif err != nil { return err }\n}","preventionTips":["Always build Option from codec constants, never string literals","Prefer geerpc.DefaultOption or passing nil to Dial","Register custom codecs in init() on both client and server","Validate CodecType against NewCodecFuncMap before dialing"],"tags":["rpc","configuration","codec"],"backgroundTag":"invalid-codec-type","analyzedSha":"cf3644382101dc13e7fd92e8f5c66cabc51bcd3b","analyzedAt":"2026-09-03T18:31:24.087Z","contentChangedAt":"2026-09-03T18:31:24.087Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}