{"record":{"id":"d944b0ba26ce0e49","repo":"geektutu/7days-golang","slug":"invalid-codec-type-s-d944b0","errorCode":null,"errorMessage":"invalid codec type %s","messagePattern":"invalid codec type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-rpc/day7-registry/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/day7-registry/client.go#L198-L234","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nopt := &geerpc.Option{ConnectTimeout: time.Second} // CodecType missing\n// after\nopt := geerpc.DefaultOption // CodecType: \"gee-gob\"","handlingStrategy":"validation","validationCode":"if _, ok := codec.NewCodecFuncMap[opt.CodecType]; !ok {\n    opt = geerpc.DefaultOption // or set a registered CodecType\n}","typeGuard":null,"tryCatchPattern":"client, err := geerpc.NewClient(conn, opt)\nif err != nil {\n    if strings.Contains(err.Error(), \"invalid codec type\") {\n        // retry once with DefaultOption\n        client, err = geerpc.NewClient(conn, geerpc.DefaultOption)\n    }\n    if err != nil { return err }\n}","preventionTips":["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."],"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"}