{"record":{"id":"c72c87f9c5ad5d34","repo":"apache/beam","slug":"2xx-codes-should-not-be-exceptions-got-status-code-s-with","errorCode":null,"errorMessage":"2xx codes should not be exceptions. Got status code: %s with body: %s","messagePattern":"2xx codes should not be exceptions\\. Got status code: (.+?) with body: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/healthcare/HttpHealthcareApiClient.java","lineNumber":596,"sourceCode":"        .datasets()\n        .fhirStores()\n        .fhir()\n        .executeBundle(fhirStore, httpBody)\n        .execute();\n  }\n\n  /**\n   * Wraps {@link HttpResponse} in an exception with a statusCode field for use with {@link\n   * HealthcareIOError}.\n   */\n  public static class HealthcareHttpException extends Exception {\n    private final int statusCode;\n\n    private HealthcareHttpException(int statusCode, String message) {\n      super(message);\n      this.statusCode = statusCode;\n      if (statusCode / 100 == 2) {\n        throw new IllegalArgumentException(\n            String.format(\n                \"2xx codes should not be exceptions. Got status code: %s with body: %s\",\n                statusCode, message));\n      }\n    }\n\n    /**\n     * Creates an exception from a non-OK response.\n     *\n     * @param statusCode the HTTP status code.\n     * @param message the error message.\n     * @return the healthcare http exception\n     */\n    static HealthcareHttpException of(int statusCode, String message) {\n      return new HealthcareHttpException(statusCode, message);\n    }\n\n    int getStatusCode() {","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/healthcare/HttpHealthcareApiClient.java#L578-L614","documentation":"HealthcareApiClient.HealthcareHttpException represents an HTTP error response from the Healthcare API. Its private constructor guards its own invariant: a 2xx status code must never be wrapped as an exception. If constructed with a 2xx code, it throws IllegalArgumentException — a bug guard indicating the caller misclassified a successful response as an error.","triggerScenarios":"Code constructs HealthcareHttpException with a status code in the 200–299 range together with a response body — i.e., an error-handling path was invoked for an actually successful response.","commonSituations":"Custom status-code handling logic that checks only 'code != 200' or misparses the status before wrapping; refactored client code where a success path is accidentally routed to error construction; tests or wrappers building the exception manually with canned status codes.","solutions":["Fix the caller so it only constructs HealthcareHttpException for status codes >= 300 (or non-2xx).","Guard before construction: if (statusCode / 100 != 2) throw new HealthcareHttpException(...).","If you see this at runtime, log the status code and body and check where the status is parsed — the response was actually successful.","Prefer a helper like fromResponse(response) that classifies status codes once, centrally."],"exampleFix":"// before\nthrow new HealthcareApiClient.HealthcareHttpException(response.getStatusCode(), body); // throws if 2xx\n\n// after\nif (response.getStatusCode() / 100 != 2) {\n  throw new HealthcareApiClient.HealthcareHttpException(response.getStatusCode(), body);\n}","handlingStrategy":"try-catch","validationCode":"if (statusCode / 100 == 2) {\n  // success: do not build HealthcareHttpException\n  return;\n}","typeGuard":null,"tryCatchPattern":"try {\n  throw new HealthcareApiClient.HealthcareHttpException(statusCode, body);\n} catch (IllegalArgumentException e) {\n  LOG.error(\"Status {} was a success code; error path misclassified\", statusCode, e);\n}","preventionTips":["Only construct the exception for non-2xx statuses.","Centralize status-code classification in one helper.","If this fires, your caller's success/error branching is inverted — fix that code."],"tags":["java","google-cloud-healthcare","http","illegal-argument","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}