Skip to content

Commit

Permalink
Add error logging for non-200 HTTP responses in JsonResponseHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrimes committed Feb 20, 2025
1 parent a01bde7 commit 0601641
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/au/csiro/http/JsonResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ public class JsonResponseHandler<T> implements ResponseHandler<T> {
*/
@Override
public T handleResponse(final HttpResponse response) throws IOException {

final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
return produceResponse(response);
} else {
// Log error response before throwing exception
String errorBody = Optional.ofNullable(response.getEntity())
.flatMap(this::quietBodyAsString)
.orElse("<no body>");
log.error("Request failed with status {}: {}", statusCode, errorBody);
throw new ClientProtocolException("Unexpected status code: " + statusCode);
}
}
Expand Down

0 comments on commit 0601641

Please sign in to comment.