{"record":{"id":"b605ad608b09609e","repo":"geektutu/7days-golang","slug":"invalid-codec-type-s-b605ad","errorCode":null,"errorMessage":"invalid codec type %s","messagePattern":"invalid codec type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-rpc/day6-load-balance/client.go","lineNumber":216,"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":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-rpc/day6-load-balance/client.go#L198-L234","documentation":"NewClient looks up the codec constructor in codec.NewCodecFuncMap by opt.CodecType and fails fast when no codec is registered for that type. Only codec types registered on both client and server (e.g. gee-gob) are supported. This prevents attempting to speak an unknown wire protocol.","triggerScenarios":"Calling NewClient (directly or via Dial/DialHTTP) with an Option whose CodecType is empty or names an unregistered codec (e.g. 'gob' instead of the registered 'gee-gob').","commonSituations":"Constructing &Option{} manually and forgetting to set CodecType or call default initialization; using NewHTTPCodec default opts whose codec differs from server; version mismatch where client/server register different codec names.","solutions":["Use geerpc.DefaultOption or a properly initialized Option whose CodecType matches a registered codec (default 'gee-gob').","Ensure the codec package's init() registering the codec in NewCodecFuncMap is imported on the client.","Make client and server agree on the same CodecType string."],"exampleFix":"// before\nopt := &geerpc.Option{} // CodecType empty\nclient, _ := geerpc.NewClient(conn, opt)\n// after\nopt := geerpc.DefaultOption\nclient, _ := geerpc.NewClient(conn, opt)","handlingStrategy":"validation","validationCode":"if codec.NewCodecFuncMap == nil || func() bool {\n    _, ok := codec.NewCodecFuncMap[opt.CodecType]\n    return !ok\n}() {\n    // use DefaultOption or fix CodecType before NewClient\n}\n// simpler: always start from a default option\nopt := geerpc.DefaultOption","typeGuard":null,"tryCatchPattern":"client, err := geerpc.NewClient(conn, opt)\nif err != nil {\n    if strings.Contains(err.Error(), \"invalid codec type\") {\n        log.Fatalf(\"codec %q not registered — import the codec package or use DefaultOption\", opt.CodecType)\n    }\n    return err\n}","preventionTips":["Always base custom Options on geerpc.DefaultOption and change only needed fields.","Never hand-write &Option{} without setting CodecType.","Import the codec implementation package (blank import if needed) so init() registers it.","Keep client/server codec names in sync via shared constants."],"tags":["rpc","codec","configuration"],"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"}