{"record":{"id":"98c7a38f14e8dfdc","repo":"hashicorp/terraform","slug":"r-status","errorCode":null,"errorMessage":"r.Status","messagePattern":"r\\.Status","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloud/backend_common.go","lineNumber":758,"sourceCode":"\tcase 404:\n\t\treturn tfe.ErrResourceNotFound\n\t}\n\n\terrs, err = decodeErrorPayload(r)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn errors.New(strings.Join(errs, \"\\n\"))\n}\n\nfunc decodeErrorPayload(r *http.Response) ([]string, error) {\n\t// Decode the error payload.\n\tvar errs []string\n\terrPayload := &jsonapi.ErrorsPayload{}\n\terr := json.NewDecoder(r.Body).Decode(errPayload)\n\tif err != nil || len(errPayload.Errors) == 0 {\n\t\treturn errs, errors.New(r.Status)\n\t}\n\n\t// Parse and format the errors.\n\tfor _, e := range errPayload.Errors {\n\t\tif e.Detail == \"\" {\n\t\t\terrs = append(errs, e.Title)\n\t\t} else {\n\t\t\terrs = append(errs, fmt.Sprintf(\"%s\\n\\n%s\", e.Title, e.Detail))\n\t\t}\n\t}\n\n\treturn errs, nil\n}\n\nfunc isValidAppName(name string) bool {\n\treturn name == \"HCP Terraform\" || name == \"Terraform Enterprise\"\n}\n","sourceCodeStart":740,"sourceCodeEnd":776,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/cloud/backend_common.go#L740-L776","documentation":"Fallback error in decodeErrorPayload (cloud/backend_common.go:758). When the response body cannot be JSON-decoded into jsonapi.ErrorsPayload, or decodes but contains zero errors, the function returns errors.New(r.Status) — i.e. the raw HTTP status line (e.g. '500 Internal Server Error'). This happens when TFE/HCP returns a non-JSON or empty error body.","triggerScenarios":"A non-401/404 TFE/HCP API response whose body is empty, not valid JSON, or a JSON shape without an `errors` array — json.Decode fails or len(errPayload.Errors)==0, so the decoder falls back to r.Status.","commonSituations":"Reverse proxy/gateway returning an HTML error page (e.g. 502 from a load balancer). Server 5xx with empty body. Network middleware intercepting the request. Misconfigured hostname pointing at a non-TFE service.","solutions":["Treat the bare status (e.g. 502/503/500) as a server/infrastructure problem and retry shortly.","Verify the backend hostname points at the real TFE/HCP endpoint and no proxy is rewriting responses.","Check TFE/HCP status page and server logs for the matching request."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":"func isRawHTTPStatus(err error) bool {\n    // decodeErrorPayload falls back to errors.New(r.Status) when body is non-JSON.\n    s := err.Error()\n    return strings.HasSuffix(s, \" Internal Server Error\") ||\n           strings.Contains(s, \"Bad Gateway\") || strings.Contains(s, \"Service Unavailable\")\n}","tryCatchPattern":"err := callTFE()\nif err != nil {\n    if isRawHTTPStatus(err) { // body was empty/non-JSON → likely infra\n        return retryWithBackoff()\n    }\n    return err\n}","preventionTips":["Verify the backend hostname points at the real TFE/HCP endpoint.","Watch for intercepting proxies returning HTML error pages.","Treat bare HTTP statuses as infrastructure errors and retry."],"tags":["terraform","cloud-backend","tfe","http","api","error-handling","infrastructure"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}