Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ private <T, V> 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)) {
Expand All @@ -142,6 +143,17 @@ private <T, V> 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 <V> HttpUriRequest buildRequest(String httpMethod, String url, V data, HttpRequestConfig config) {
RequestBuilder requestBuilder = RequestBuilder.create(httpMethod);
requestBuilder.setUri(URI.create(this.appendUrlParams(url, config.getUrlParams())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<html><body>502 Bad Gateway</body></html>";
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
Expand Down
Loading