Skip to content

Commit 150ff3d

Browse files
EnsnerTbaywet
andauthored
fix: errorMapping in ResponseCodes with "4XX" and "5XX" Pattern (#1735)
* 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]>
1 parent d898436 commit 150ff3d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/main/java/com/microsoft/graph/core/requests/ResponseBodyHandler.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private void handleFailedResponse(Response nativeResponse, HashMap<String, Parsa
7474
int statusCode = nativeResponse.code();
7575
String statusCodeString = String.valueOf(statusCode);
7676
if (errorMappings == null ||
77-
!errorMappings.containsKey(statusCodeString) ||
77+
!errorMappings.containsKey(statusCodeString) &&
7878
!(statusCode >= 400 && statusCode <= 499 && errorMappings.containsKey("4XX")) &&
7979
!(statusCode >= 500 && statusCode <= 599 && errorMappings.containsKey("5XX"))) {
8080
throw new ApiExceptionBuilder()
@@ -83,7 +83,15 @@ private void handleFailedResponse(Response nativeResponse, HashMap<String, Parsa
8383
.withResponseHeaders(HeadersCompatibility.getResponseHeaders(nativeResponse.headers()))
8484
.build();
8585
} else {
86-
Parsable result = parseNode.getObjectValue(errorMappings.get(statusCodeString));
86+
String statusCodePattern = statusCodeString;
87+
if (!errorMappings.containsKey(statusCodePattern)) {
88+
if (statusCode >= 400 && statusCode <= 499 && errorMappings.containsKey("4XX")) {
89+
statusCodePattern = "4XX";
90+
} else if (statusCode >= 500 && statusCode <= 599 && errorMappings.containsKey("5XX")) {
91+
statusCodePattern = "5XX";
92+
}
93+
}
94+
Parsable result = parseNode.getObjectValue(errorMappings.get(statusCodePattern));
8795
if (!(result instanceof Exception)) {
8896
throw new ApiException("The server returned an unexpected status code and the error registered for this code failed to deserialize: " + statusCodeString);
8997
}

0 commit comments

Comments
 (0)