From 34671274a4c00b73f44ac740d8af8e61bfba6acd Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2026 01:28:22 -0400 Subject: [PATCH] fix: keep status code and body for non-JSON error responses Co-Authored-By: Claude Sonnet 5.5 --- .../core/http/impl/http/ApacheHttpClient.java | 16 +++++++-- .../http/impl/http/ApacheHttpClientTest.java | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/crowdin/client/core/http/impl/http/ApacheHttpClient.java b/src/main/java/com/crowdin/client/core/http/impl/http/ApacheHttpClient.java index 7f95c04d5..0dd741eeb 100644 --- a/src/main/java/com/crowdin/client/core/http/impl/http/ApacheHttpClient.java +++ b/src/main/java/com/crowdin/client/core/http/impl/http/ApacheHttpClient.java @@ -124,8 +124,9 @@ private T request(String url, try (CloseableHttpResponse response = httpClient.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode < 200 || statusCode >= 300) { - String error = this.toString(response.getEntity()); - throw this.jsonTransformer.parse(error, CrowdinApiException.class); + HttpEntity entity = response.getEntity(); + httpResponse = entity == null ? null : this.toString(entity); + throw this.toException(statusCode, httpResponse); } //plain response if (String.class.equals(clazz)) { @@ -142,6 +143,17 @@ private T request(String url, } } + private CrowdinApiException toException(int statusCode, String body) { + try { + return this.jsonTransformer.parse(body, CrowdinApiException.class); + } catch (Exception e) { + // the body is missing or is not JSON, e.g. an HTML page from a gateway + HttpException exception = HttpException.fromMessage("HTTP error " + statusCode, body); + exception.getError().setCode(String.valueOf(statusCode)); + return exception; + } + } + private HttpUriRequest buildRequest(String httpMethod, String url, V data, HttpRequestConfig config) { RequestBuilder requestBuilder = RequestBuilder.create(httpMethod); requestBuilder.setUri(URI.create(this.appendUrlParams(url, config.getUrlParams()))); diff --git a/src/test/java/com/crowdin/client/core/http/impl/http/ApacheHttpClientTest.java b/src/test/java/com/crowdin/client/core/http/impl/http/ApacheHttpClientTest.java index a16645072..4663fa4b4 100644 --- a/src/test/java/com/crowdin/client/core/http/impl/http/ApacheHttpClientTest.java +++ b/src/test/java/com/crowdin/client/core/http/impl/http/ApacheHttpClientTest.java @@ -115,6 +115,42 @@ public void testGetRequestError() throws IOException { assertThrows(HttpBadRequestException.class, () -> httpClient.get(organizationUrl, new HttpRequestConfig(), String.class)); } + @Test + public void testErrorWithNonJsonBodyKeepsStatusAndBody() throws IOException { + String body = "502 Bad Gateway"; + when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse); + when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_GATEWAY, "Bad Gateway")); + when(mockedResponse.getEntity()).thenReturn(new StringEntity(body)); + + HttpException exception = assertThrows(HttpException.class, () -> httpClient.get(organizationUrl, new HttpRequestConfig(), String.class)); + + assertEquals("502", exception.getError().getCode()); + assertEquals(body, exception.getHttpResponse()); + } + + @Test + public void testErrorWithEmptyBodyKeepsStatus() throws IOException { + when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse); + when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "Unauthorized")); + when(mockedResponse.getEntity()).thenReturn(new StringEntity("")); + + HttpException exception = assertThrows(HttpException.class, () -> httpClient.get(organizationUrl, new HttpRequestConfig(), String.class)); + + assertEquals("401", exception.getError().getCode()); + assertEquals("", exception.getHttpResponse()); + } + + @Test + public void testErrorWithoutEntityKeepsStatus() throws IOException { + when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse); + when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "Not Found")); + + HttpException exception = assertThrows(HttpException.class, () -> httpClient.head(organizationUrl, new HttpRequestConfig(), Void.class)); + + assertEquals("404", exception.getError().getCode()); + assertNull(exception.getHttpResponse()); + } + @Test public void testDeleteRequest() throws IOException { // Mock the behavior of client.execute to return mockedResponse