Skip to content

Commit

Permalink
fix: errorMapping in ResponseCodes with "4XX" and "5XX" Pattern (#1735)
Browse files Browse the repository at this point in the history
* Enable errorMapping with "4XX" and "5XX" pattern

fix: Failed Responses now work with errorMapping with Key "4XX" and "5XX".

It looks like, there should be code here to satisfy the "4XX" and "5XX" keys to match 400-499 and 500-599 StatusCodes. But it does not work yet, so i implemented it quickly. 

Im new here in such big Projects, so i dont know if im at the right address for this issue. I'm open for feedback tho.

* fix: missing parenthesis

---------

Co-authored-by: Vincent Biret <[email protected]>
  • Loading branch information
EnsnerT and baywet authored Sep 9, 2024
1 parent d898436 commit 150ff3d
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void handleFailedResponse(Response nativeResponse, HashMap<String, Parsa
int statusCode = nativeResponse.code();
String statusCodeString = String.valueOf(statusCode);
if (errorMappings == null ||
!errorMappings.containsKey(statusCodeString) ||
!errorMappings.containsKey(statusCodeString) &&
!(statusCode >= 400 && statusCode <= 499 && errorMappings.containsKey("4XX")) &&
!(statusCode >= 500 && statusCode <= 599 && errorMappings.containsKey("5XX"))) {
throw new ApiExceptionBuilder()
Expand All @@ -83,7 +83,15 @@ private void handleFailedResponse(Response nativeResponse, HashMap<String, Parsa
.withResponseHeaders(HeadersCompatibility.getResponseHeaders(nativeResponse.headers()))
.build();
} else {
Parsable result = parseNode.getObjectValue(errorMappings.get(statusCodeString));
String statusCodePattern = statusCodeString;
if (!errorMappings.containsKey(statusCodePattern)) {
if (statusCode >= 400 && statusCode <= 499 && errorMappings.containsKey("4XX")) {
statusCodePattern = "4XX";
} else if (statusCode >= 500 && statusCode <= 599 && errorMappings.containsKey("5XX")) {
statusCodePattern = "5XX";
}
}
Parsable result = parseNode.getObjectValue(errorMappings.get(statusCodePattern));
if (!(result instanceof Exception)) {
throw new ApiException("The server returned an unexpected status code and the error registered for this code failed to deserialize: " + statusCodeString);
}
Expand Down

0 comments on commit 150ff3d

Please sign in to comment.