Skip to content

Commit a2b3eae

Browse files
committed
Add enum for HTTP status codes and helper methods
1 parent 8479e03 commit a2b3eae

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/DefaultRetryPolicy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class DefaultRetryPolicy implements IRetryPolicy {
1212

1313
@Override
1414
public boolean isRetryable(IHttpResponse httpResponse) {
15-
return httpResponse.statusCode() >= 500 &&
16-
httpResponse.statusCode() < 600 &&
15+
return HttpStatus.isServerError(httpResponse.statusCode()) &&
1716
HttpHelper.getRetryAfterHeader(httpResponse) == null;
1817
}
1918

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.aad.msal4j;
5+
6+
enum HttpStatus {
7+
OK(200, "OK"),
8+
FOUND(302, "Found"),
9+
BAD_REQUEST(400, "Bad Request"),
10+
NOT_FOUND(404, "Not Found"),
11+
REQUEST_TIMEOUT(408, "Request Timeout"),
12+
GONE(410, "Gone"),
13+
TOO_MANY_REQUESTS(429, "Too Many Requests"),
14+
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
15+
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
16+
GATEWAY_TIMEOUT(504, "Gateway Timeout");
17+
18+
private final int code;
19+
private final String description;
20+
21+
HttpStatus(int code, String description) {
22+
this.code = code;
23+
this.description = description;
24+
}
25+
26+
int getCode() {
27+
return code;
28+
}
29+
30+
String getDescription() {
31+
return description;
32+
}
33+
34+
//All 5xx errors
35+
static boolean isServerError(int code) {
36+
return code >= 500 && code < 600;
37+
}
38+
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/IMDSRetryPolicy.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public boolean isRetryable(IHttpResponse httpResponse) {
1818
currentRetryCount++;
1919
lastStatusCode = httpResponse.statusCode();
2020

21-
return (lastStatusCode >= 500 && lastStatusCode < 600) ||
22-
lastStatusCode == 404 || // Not Found
23-
lastStatusCode == 408 || // Request Timeout
24-
lastStatusCode == 410 || // Gone
25-
lastStatusCode == 429; // Too Many Requests
21+
return HttpStatus.isServerError(lastStatusCode) ||
22+
lastStatusCode == HttpStatus.NOT_FOUND.getCode() ||
23+
lastStatusCode == HttpStatus.REQUEST_TIMEOUT.getCode() ||
24+
lastStatusCode == HttpStatus.GONE.getCode() ||
25+
lastStatusCode == HttpStatus.TOO_MANY_REQUESTS.getCode();
2626
}
2727

2828
@Override

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityRetryPolicy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class ManagedIdentityRetryPolicy implements IRetryPolicy {
1414
public boolean isRetryable(IHttpResponse httpResponse) {
1515
int statusCode = httpResponse.statusCode();
1616

17-
return statusCode == 404 || // Not Found
18-
statusCode == 408 || // Request Timeout
19-
statusCode == 429 || // Too Many Requests
20-
statusCode == 500 || // Internal Server Error
21-
statusCode == 503 || // Service Unavailable
22-
statusCode == 504; // Gateway Timeout
17+
return statusCode == HttpStatus.NOT_FOUND.getCode() ||
18+
statusCode == HttpStatus.REQUEST_TIMEOUT.getCode() ||
19+
statusCode == HttpStatus.TOO_MANY_REQUESTS.getCode() ||
20+
statusCode == HttpStatus.INTERNAL_SERVER_ERROR.getCode() ||
21+
statusCode == HttpStatus.SERVICE_UNAVAILABLE.getCode() ||
22+
statusCode == HttpStatus.GATEWAY_TIMEOUT.getCode();
2323
}
2424

2525
@Override

0 commit comments

Comments
 (0)