{"record":{"id":"8a8e78cc29ef59dc","repo":"grpc/grpc-go","slug":"invalid-code-d","errorCode":null,"errorMessage":"invalid code: %d","messagePattern":"invalid code: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codes/codes.go","lineNumber":238,"sourceCode":"\t`\"DATA_LOSS\"`:           DataLoss,\n\t`\"UNAUTHENTICATED\"`:     Unauthenticated,\n}\n\n// UnmarshalJSON unmarshals b into the Code.\nfunc (c *Code) UnmarshalJSON(b []byte) error {\n\t// From json.Unmarshaler: By convention, to approximate the behavior of\n\t// Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte(\"null\")) as\n\t// a no-op.\n\tif string(b) == \"null\" {\n\t\treturn nil\n\t}\n\tif c == nil {\n\t\treturn fmt.Errorf(\"nil receiver passed to UnmarshalJSON\")\n\t}\n\n\tif ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {\n\t\tif ci >= _maxCode {\n\t\t\treturn fmt.Errorf(\"invalid code: %d\", ci)\n\t\t}\n\n\t\t*c = Code(ci)\n\t\treturn nil\n\t}\n\n\tif jc, ok := strToCode[string(b)]; ok {\n\t\t*c = jc\n\t\treturn nil\n\t}\n\treturn fmt.Errorf(\"invalid code: %q\", string(b))\n}\n","sourceCodeStart":220,"sourceCodeEnd":251,"githubUrl":"https://github.com/grpc/grpc-go/blob/03255a9237b6eb32710f6bc4f2de9a675b99fe36/codes/codes.go#L220-L251","documentation":"In codes.Code.UnmarshalJSON (codes.go:236-238), if the JSON token parses as an unsigned integer but is >= _maxCode (17), it is out of the valid gRPC code range (0..16) and rejected. The %d is the offending numeric value.","triggerScenarios":"Decoding JSON containing a numeric status code >= 17 into a codes.Code — e.g. a hand-built status JSON, a proto JSON with a wrong numeric enum value, or a custom encoder emitting an unsupported code.","commonSituations":"An upstream service serialising a non-standard code number; test fixtures with made-up codes; a proto where the enum was extended beyond the gRPC spec.","solutions":["Use a valid code in 0..16 (see codes.Code constants: OK=0 … Unauthenticated=16).","If the value comes from a third party, map/normalize it to a valid code before decoding.","Send the code as its string name (e.g. \"UNAVAILABLE\") which is also accepted."],"exampleFix":"// before\njson.Unmarshal([]byte(`99`), &c)\n// after\njson.Unmarshal([]byte(`14`), &c)  // codes.Unavailable","handlingStrategy":"validation","validationCode":"// Reject out-of-range numeric codes before decoding.\nfunc validCodeNum(n uint64) bool { return n < 17 } // _maxCode","typeGuard":"func isValidCode(c codes.Code) bool { return int(c) >= 0 && int(c) < 17 }","tryCatchPattern":null,"preventionTips":["Validate numeric codes against the 0..16 range at the boundary that produces them.","Send codes as their canonical string names when unsure of numbering.","Map vendor-specific codes to a valid gRPC code before serialization."],"tags":["go","grpc","codes","json","validation"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}