{"record":{"id":"6ec3595a8a476f04","repo":"grpc/grpc-go","slug":"invalid-code-q","errorCode":null,"errorMessage":"invalid code: %q","messagePattern":"invalid code: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codes/codes.go","lineNumber":249,"sourceCode":"\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":231,"sourceCodeEnd":251,"githubUrl":"https://github.com/grpc/grpc-go/blob/03255a9237b6eb32710f6bc4f2de9a675b99fe36/codes/codes.go#L231-L251","documentation":"In codes.Code.UnmarshalJSON (codes.go:245-249), if the JSON token is not a number and does not match any key in strToCode (the map of \"OK\",\"CANCELLED\",…,\"UNAUTHENTICATED\"), it is rejected. The %q is the unrecognized string (including any surrounding quotes from the raw JSON).","triggerScenarios":"Decoding JSON whose code field is a string that isn't a known gRPC code name — typos like \"Unavailable\" (wrong case) or \"FAILED\", or a completely foreign value.","commonSituations":"Case mismatch (codes are upper-snake-case: \"UNAVAILABLE\"); a non-gRPC enum string; a legacy/vendored schema using different naming.","solutions":["Use the exact upper-snake-case name from the gRPC spec, e.g. \"UNAVAILABLE\".","If the source uses different casing, normalize it to upper-snake-case before unmarshalling.","Alternatively send the numeric value (0..16) which is also accepted."],"exampleFix":"// before\njson.Unmarshal([]byte(`\"Unavailable\"`), &c)\n// after\njson.Unmarshal([]byte(`\"UNAVAILABLE\"`), &c)","handlingStrategy":"validation","validationCode":"// Normalize a code string to upper-snake-case and check it is known.\nfunc normalizeCodeName(s string) (string, bool) {\n    up := strings.ToUpper(strings.TrimSpace(s))\n    known := map[string]bool{\"OK\":true,\"CANCELLED\":true,\"UNKNOWN\":true,\"INVALID_ARGUMENT\":true,\"DEADLINE_EXCEEDED\":true,\"NOT_FOUND\":true,\"ALREADY_EXISTS\":true,\"PERMISSION_DENIED\":true,\"RESOURCE_EXHAUSTED\":true,\"FAILED_PRECONDITION\":true,\"ABORTED\":true,\"OUT_OF_RANGE\":true,\"UNIMPLEMENTED\":true,\"INTERNAL\":true,\"UNAVAILABLE\":true,\"DATA_LOSS\":true,\"UNAUTHENTICATED\":true}\n    return up, known[up]\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the exact upper-snake-case names from the gRPC spec.","Normalize casing at the producer side before emitting code strings.","Fall back to the numeric form if naming is uncertain."],"tags":["go","grpc","codes","json","validation"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}