{"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/0c51461d27177d997e14c642fe18c11668fc09a3/codes/codes.go#L231-L251","documentation":"Returned by codes.Code.UnmarshalJSON when the JSON input is a string token (with surrounding quotes) that does not match any known canonical gRPC code name in the strToCode map (OK, CANCELLED, UNKNOWN, INVALID_ARGUMENT, DEADLINE_EXCEEDED, NOT_FOUND, ALREADY_EXISTS, PERMISSION_DENIED, RESOURCE_EXHAUSTED, FAILED_PRECONDITION, ABORTED, OUT_OF_RANGE, UNIMPLEMENTED, INTERNAL, UNAVAILABLE, DATA_LOSS, UNAUTHENTICATED).","triggerScenarios":"Unmarshaling JSON containing a quoted string that is not one of the 17 recognized code names — e.g. \"FOO\", \"CANCEL\" (missing an L; the canonical name is CANCELLED), \"not_found\" (lowercase), or \"5\" (a number inside quotes, which ParseUint rejects due to the quotes and strToCode also rejects).","commonSituations":"A typo in the code name, using lowercase instead of UPPER_SNAKE_CASE, using a different naming convention (e.g. HTTP status text like \"NOT_FOUND\" is valid but \"Not Found\" is not), or a non-gRPC service emitting its own string error identifiers into the same field.","solutions":["Use the exact canonical UPPER_SNAKE_CASE name from the strToCode map (note CANCELLED is British spelling with two L's).","Send numeric codes as bare JSON numbers (no quotes) within [0,16] instead of strings.","Validate/gateway external string values against codes.Code.String() or a known enum before producing the JSON."],"exampleFix":"// before\nvar c codes.Code\njson.Unmarshal([]byte(`\"CANCEL\"`), &c) // invalid code: \"CANCEL\" (missing an L)\n\n// after\nvar c codes.Code\njson.Unmarshal([]byte(`\"CANCELLED\"`), &c) // canonical name","handlingStrategy":"validation","validationCode":"var validCodeNames = map[string]bool{\n    `\"OK\"`: true, `\"CANCELLED\"`: true, `\"UNKNOWN\"`: true, `\"INVALID_ARGUMENT\"`: true,\n    `\"DEADLINE_EXCEEDED\"`: true, `\"NOT_FOUND\"`: true, `\"ALREADY_EXISTS\"`: true,\n    `\"PERMISSION_DENIED\"`: true, `\"RESOURCE_EXHAUSTED\"`: true, `\"FAILED_PRECONDITION\"`: true,\n    `\"ABORTED\"`: true, `\"OUT_OF_RANGE\"`: true, `\"UNIMPLEMENTED\"`: true, `\"INTERNAL\"`: true,\n    `\"UNAVAILABLE\"`: true, `\"DATA_LOSS\"`: true, `\"UNAUTHENTICATED\"`: true,\n}\nfunc isValidCodeJSONToken(b []byte) bool {\n    _, ok1 := strconv.ParseUint(string(b), 10, 32) // numeric ok if < 17\n    return ok1 == nil || validCodeNames[string(b)]\n}","typeGuard":"func isCanonicalCodeName(s string) bool {\n    return validCodeNames[strconv.Quote(s)]\n}","tryCatchPattern":null,"preventionTips":["Always use the exact canonical UPPER_SNAKE_CASE names; remember CANCELLED is British spelling.","Prefer numeric encoding (bare JSON number in [0,16]) for machine-to-machine interchange.","Generate code names from codes.Code.String() rather than hand-typing them."],"tags":["golang","json","grpc-codes","validation","typo"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}