{"record":{"id":"21bf1b2f5e8bafa1","repo":"grafana/k6","slug":"unexpected-http-error-from-s-d-s-21bf1b","errorCode":null,"errorMessage":"unexpected HTTP error from %s: %d %s","messagePattern":"unexpected HTTP error from (.+?): (.+?) (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloudapi/v6/client.go","lineNumber":91,"sourceCode":"\t\treturn errUnknown\n\t}\n\n\tif c := r.StatusCode; c >= 200 && c <= 299 {\n\t\treturn nil\n\t}\n\n\tdata, err := io.ReadAll(r.Body)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tvar respErr error\n\tvar payload ResponseError\n\tif err := json.Unmarshal(data, &payload); err != nil {\n\t\tif classified := httperr.ClassifyStatus(r.StatusCode); classified != nil {\n\t\t\trespErr = classified\n\t\t} else {\n\t\t\trespErr = fmt.Errorf(\n\t\t\t\t\"unexpected HTTP error from %s: %d %s\",\n\t\t\t\tr.Request.URL,\n\t\t\t\tr.StatusCode,\n\t\t\t\thttp.StatusText(r.StatusCode),\n\t\t\t)\n\t\t}\n\t} else {\n\t\tpayload.Response = r\n\t\trespErr = payload\n\t}\n\n\tif r.StatusCode == http.StatusUnauthorized {\n\t\treturn errext.WithHint(respErr,\n\t\t\t\"Authenticate by running `k6 cloud login` \"+\n\t\t\t\t\"or verify the active Grafana Cloud token (K6_CLOUD_TOKEN, options.cloud.token)\")\n\t}\n\treturn respErr\n}","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/cloudapi/v6/client.go#L73-L109","documentation":"CheckResponse got a non-2xx response whose body could not be unmarshalled as the API's JSON error payload, and httperr.ClassifyStatus had no mapping for that status code. The fallback error names the requesting URL, numeric status, and status text so you can see which endpoint failed and how — but the server's human-readable reason (usually HTML or empty) is lost.","triggerScenarios":"A reverse proxy, load balancer, or captive portal returning an HTML error page (502/503/499), an empty body with an unusual status (520-527 from Cloudflare), or any non-JSON body for a status ClassifyStatus does not special-case.","commonSituations":"Corporate proxies intercepting requests to the cloud host, transient gateway errors during Grafana Cloud incidents, self-hosted misconfigured gateways, or WAFs blocking the k6 user-agent with a 406/418 HTML page.","solutions":["Retry after a short delay — gateway/proxy errors are frequently transient; the SDK's built-in retry only covers some paths","Reproduce outside k6 (curl -v the same URL with your token) to see the raw body the gateway returns","If a proxy is the source, bypass it for the cloud host or fix its error pages / allowlisting","If it persists, capture r.Request.URL and status from the message and report to Grafana Cloud support with the timestamp"],"exampleFix":"// before\nerr := client.StartTest(ctx, id)\n// message: unexpected HTTP error from https://...: 502 Bad Gateway\n\n// after\n// wrap and preserve the body for diagnosis\ndec, _ := io.ReadAll(resp.Body)\nreturn fmt.Errorf(\"%w; body preview: %.200s\", err, string(dec))","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func isResponseError(err error) (*cloudapiv6.ResponseError, bool) {\n    var re *cloudapiv6.ResponseError\n    if errors.As(err, &re) { return re, true }\n    return nil, false\n}","tryCatchPattern":"err := client.StartTest(ctx, id)\nif err != nil {\n    if _, ok := isResponseError(err); !ok && strings.Contains(err.Error(), \"unexpected HTTP error\") {\n        // non-JSON gateway body: almost always transient proxy/LB trouble\n        time.Sleep(10 * time.Second)\n        err = client.StartTest(ctx, id)\n    }\n    if err != nil { return fmt.Errorf(\"starting test: %w\", err) }\n}","preventionTips":["Expect HTML/empty bodies from proxies; retry gateway-class statuses before escalating","Capture the raw body in debug builds to identify which middlebox produced it","Keep cloud hosts on your proxy allowlist so error pages never originate in-network"],"tags":["http-response","gateway","proxy","json-parsing","cloud-api"],"backgroundTag":"unexpected-http-response","analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-31T22:51:04.189Z","contentChangedAt":"2026-08-31T22:51:04.189Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}