{"record":{"id":"42c22cef00ec3746","repo":"apple/pkl","slug":"badhttpstatuscode-42c22c","errorCode":"badHttpStatusCode","errorMessage":"Received unexpected status code `{0}` when making request `GET {1}`.","messagePattern":"Received unexpected status code `(.+?)` when making request `GET (.+?)`\\.","errorType":"error_code","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/util/HttpUtils.java","lineNumber":60,"sourceCode":"  }\n\n  public static boolean isHttpUrl(URI uri) {\n    var scheme = uri.getScheme();\n    return \"https\".equalsIgnoreCase(scheme) || \"http\".equalsIgnoreCase(scheme);\n  }\n\n  public static void checkHasStatusCode200(HttpResponse<?> response) throws IOException {\n    if (response.statusCode() == 200) return;\n\n    var body = response.body();\n    if (body instanceof AutoCloseable closeable) {\n      try {\n        closeable.close();\n      } catch (Exception ignored) {\n      }\n    }\n\n    throw new IOException(\n        ErrorMessages.create(\"badHttpStatusCode\", response.statusCode(), response.uri()));\n  }\n\n  public static URI setPort(URI uri, int port) {\n    if (port < 0 || port > 65535) {\n      throw new IllegalArgumentException(String.valueOf(port));\n    }\n    try {\n      return new URI(\n          uri.getScheme(),\n          uri.getUserInfo(),\n          uri.getHost(),\n          port,\n          uri.getPath(),\n          uri.getQuery(),\n          uri.getFragment());\n    } catch (URISyntaxException e) {\n      throw PklBugException.unreachableCode(); // only port changed","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/util/HttpUtils.java#L42-L78","documentation":"HttpUtils.checkHasStatusCode200 verifies an HTTP response returned 200 and otherwise throws an IOException carrying the badHttpStatusCode message with the actual status code and request URI. It indicates the remote server responded, but not with the expected 200 OK.","triggerScenarios":"Any HTTP fetch routed through HttpUtils (module/resource downloads over http(s), dependency resolution) where the server returns a non-200 status such as 404, 403, 500, or a redirect that is not followed.","commonSituations":"Wrong or moved URL (404), missing/invalid credentials (401/403), server-side errors (5xx), rate limiting (429), or a proxy intercepting the request.","solutions":["Check the printed status code and URI: fix the URL if 404, or supply credentials if 401/403","Open the URI in a browser/curl to see the server's error body for details","Retry later if 429/5xx, and check proxy/firewall interference","Point the import/dependency at a version or path that exists"],"exampleFix":"// before\n// importing https://example.com/missing.pkl -> 404\nimport \"https://example.com/missing.pkl\"\n// after\nimport \"https://example.com/present.pkl\"","handlingStrategy":"try-catch","validationCode":"const res = await fetch(url, { method: 'HEAD' });\nif (res.status !== 200) throw new Error(`Precheck failed: ${res.status} for ${url}`);","typeGuard":null,"tryCatchPattern":"try {\n  return fetchModule(uri);\n} catch (IOException e) {\n  if (e.getMessage().startsWith(\"Received unexpected status code\")) {\n    // parse status from message; handle 404 (missing), 401/403 (auth), 5xx (retry)\n  } else throw e;\n}","preventionTips":["Check the URL exists and is reachable before importing (curl -I)","Provide credentials for private endpoints","Expect and handle 429/5xx with backoff","Pin dependency versions/URLs that are known-good"],"tags":["http","network","status-code"],"backgroundTag":"http-error-status","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}