Skip to content

Commit 06eb0d3

Browse files
authored
Merge pull request #53 from Fugasss/steam-api-exception-status-code-exposure
SteamApiException's status code exposure
2 parents 14c8385 + 05c79b8 commit 06eb0d3

5 files changed

Lines changed: 68 additions & 90 deletions

File tree

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
11
package com.lukaspradel.steamapi.core.exception;
22

3+
import com.lukaspradel.steamapi.webapi.request.HttpStatus;
4+
35
public class SteamApiException extends Exception {
46

57
private static final long serialVersionUID = 6414882632273395318L;
68

7-
public enum Cause {
8-
HTTP_ERROR, FORBIDDEN, INTERNAL_ERROR, MAPPING
9-
}
10-
119
private final String message;
10+
private final Integer statusCode;
1211

13-
public SteamApiException(Cause cause, Throwable exceptionCause) {
14-
12+
public SteamApiException(Throwable exceptionCause) {
1513
super(exceptionCause);
1614

17-
switch (cause) {
18-
case MAPPING:
19-
this.message = "The JSON response could not be parsed or mapped to the designated POJO. The most likely cause for this is that"
20-
+ " the Steam API itself changed. Check for newer versions of this library to compensate for this.";
21-
break;
22-
default:
23-
this.message = "The Web API request failed due to an unexpected error: "
24-
+ exceptionCause.getMessage();
25-
}
15+
this.message = "The Web API request failed due to an unexpected error: " + exceptionCause.getMessage();
16+
this.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
2617
}
2718

28-
public SteamApiException(Cause cause, Integer statusCode) {
29-
30-
switch (cause) {
31-
case HTTP_ERROR:
32-
this.message = "The Web API request failed (status code: " + statusCode + ").";
33-
break;
34-
case FORBIDDEN:
35-
this.message = "The Web API request failed for security reasons. The supplied Web API key was rejected by Steam. Ensure that the supplied Web API key is valid.";
36-
break;
37-
case INTERNAL_ERROR:
38-
this.message = "The Web API request failed with an internal error (status code: " + statusCode + ").";
39-
break;
40-
default:
41-
this.message = "The Web API request failed due to an unexpected error.";
42-
}
19+
public SteamApiException(int statusCode) {
20+
this.statusCode = statusCode;
21+
this.message = getMessageByStatusCode(statusCode);
4322
}
4423

4524
@Override
@@ -50,4 +29,21 @@ public String getMessage() {
5029
return this.message;
5130
}
5231
}
32+
33+
public Integer getStatusCode() {
34+
return statusCode;
35+
}
36+
37+
private static String getMessageByStatusCode(int statusCode) {
38+
39+
if (statusCode == HttpStatus.BAD_REQUEST) { return "The Web API request failed. Wrong data format was provided."; }
40+
else if (statusCode == HttpStatus.FORBIDDEN) { return "The Web API request failed for security reasons. The supplied Web API key was rejected by Steam. Ensure that the supplied Web API key is valid."; }
41+
else if (statusCode == HttpStatus.NOT_FOUND) { return "Failed to found requested resource."; }
42+
else if (statusCode == HttpStatus.TOO_MANY_REQUESTS) { return "The Web API request rejected. Because too many requests were processed."; }
43+
else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR) { return "The Web API request failed with an internal error."; }
44+
else if (statusCode == HttpStatus.BAD_GATEWAY) { return "The Web API gateway failed to route request."; }
45+
else if (statusCode == HttpStatus.SERVICE_UNAVAILABLE) { return "The Web API service unavailable."; }
46+
47+
return "";
48+
}
5349
}

src/main/java/com/lukaspradel/steamapi/webapi/client/SteamWebApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public <T> T processRequest(SteamWebApiRequest request)
3838

3939
try {
4040
result = (T) MAPPER.readValue(response, request.getResponseType());
41-
} catch (IOException e) {
42-
throw new SteamApiException(SteamApiException.Cause.MAPPING, e);
41+
} catch (IOException exception) {
42+
throw new SteamApiException(exception);
4343
}
4444
return result;
4545
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.lukaspradel.steamapi.webapi.request;
2+
3+
public class HttpStatus{
4+
public static final int OK = 200;
5+
public static final int CREATED = 201;
6+
public static final int ACCEPTED = 202;
7+
public static final int NO_CONTENT = 204;
8+
public static final int BAD_REQUEST = 400;
9+
public static final int FORBIDDEN = 403;
10+
public static final int NOT_FOUND = 404;
11+
public static final int TOO_MANY_REQUESTS = 429;
12+
public static final int INTERNAL_SERVER_ERROR = 500;
13+
public static final int BAD_GATEWAY = 502;
14+
public static final int SERVICE_UNAVAILABLE = 503;
15+
16+
17+
public static boolean Is2xxStatus(int statusCode) {
18+
return statusCode >= 200 && statusCode < 300;
19+
}
20+
21+
public static boolean Is4xxStatus(int statusCode) {
22+
return statusCode >= 400 && statusCode < 500;
23+
}
24+
25+
public static boolean Is5xxStatus(int statusCode) {
26+
return statusCode >= 500 && statusCode < 600;
27+
}
28+
}

src/main/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandler.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
import java.util.Map;
1414
import java.util.stream.Stream;
1515

16-
import static com.lukaspradel.steamapi.core.exception.SteamApiException.Cause.INTERNAL_ERROR;
1716
import static java.nio.charset.StandardCharsets.UTF_8;
1817
import static java.util.stream.Collectors.joining;
1918

2019
public class SteamWebApiRequestHandler extends SteamApiRequestHandler {
2120

22-
private static final int UNAUTHORIZED = 401;
21+
2322

2423
public SteamWebApiRequestHandler(boolean useHttps, String key) {
2524
super(useHttps, key);
@@ -95,19 +94,18 @@ String getWebApiResponse(URI requestUrl) throws SteamApiException {
9594
var response = client.send(getRequest, BodyHandlers.ofString());
9695
int statusCode = response.statusCode();
9796

98-
if (Integer.toString(statusCode).startsWith("20")) {
97+
if (HttpStatus.Is2xxStatus(statusCode)) {
9998
return response.body();
100-
} else if (statusCode == UNAUTHORIZED) {
101-
throw new SteamApiException(SteamApiException.Cause.FORBIDDEN, statusCode);
102-
} else {
103-
throw new SteamApiException(SteamApiException.Cause.HTTP_ERROR, statusCode);
99+
} else{
100+
throw new SteamApiException(statusCode);
104101
}
105-
} catch (IOException | InterruptedException e) {
106-
throw new SteamApiException(INTERNAL_ERROR, e);
102+
} catch (IOException | InterruptedException exception) {
103+
throw new SteamApiException(exception);
107104
}
108105
}
109106

110107
HttpClient getHttpClient() {
111108
return HttpClient.newHttpClient();
112109
}
113110
}
111+

src/test/java/com/lukaspradel/steamapi/webapi/client/SteamWebApiClientTest.java

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -82,63 +82,20 @@ public void init() {
8282
client = spy(new SteamWebApiClient(requestHandlerMock));
8383
}
8484

85-
@Test(expectedExceptions = SteamApiException.class)
86-
public void testProcessExceptionMapping() throws SteamApiException {
87-
88-
when(requestHandlerMock.getWebApiResponse(requestMock)).thenThrow(
89-
new SteamApiException(SteamApiException.Cause.MAPPING,
90-
new Throwable()));
91-
92-
client.processRequest(requestMock);
93-
}
94-
9585
@Test(expectedExceptions = SteamApiException.class)
9686
public void testProcessExceptionUnexpectedError() throws SteamApiException {
9787

9888
when(requestHandlerMock.getWebApiResponse(requestMock)).thenThrow(
99-
new SteamApiException(SteamApiException.Cause.INTERNAL_ERROR,
100-
new Throwable()));
101-
102-
client.processRequest(requestMock);
103-
}
104-
105-
@Test(expectedExceptions = SteamApiException.class)
106-
public void testProcessExceptionHttpError() throws SteamApiException {
107-
108-
when(requestHandlerMock.getWebApiResponse(requestMock)).thenThrow(
109-
new SteamApiException(SteamApiException.Cause.HTTP_ERROR,
110-
Integer.valueOf(404)));
111-
112-
client.processRequest(requestMock);
113-
}
114-
115-
@Test(expectedExceptions = SteamApiException.class)
116-
public void testProcessExceptionForbiddenError() throws SteamApiException {
117-
118-
when(requestHandlerMock.getWebApiResponse(requestMock)).thenThrow(
119-
new SteamApiException(SteamApiException.Cause.FORBIDDEN,
120-
Integer.valueOf(403)));
121-
122-
client.processRequest(requestMock);
123-
}
124-
125-
@Test(expectedExceptions = SteamApiException.class)
126-
public void testProcessExceptionInternalError() throws SteamApiException {
127-
128-
when(requestHandlerMock.getWebApiResponse(requestMock)).thenThrow(
129-
new SteamApiException(SteamApiException.Cause.INTERNAL_ERROR,
130-
Integer.valueOf(500)));
89+
new SteamApiException(new Throwable()));
13190

13291
client.processRequest(requestMock);
13392
}
13493

13594
@Test(expectedExceptions = SteamApiException.class)
136-
public void testProcessExceptionUnexpectedStatusError()
137-
throws SteamApiException {
95+
public void testProcessExceptionStatusCode() throws SteamApiException {
13896

13997
when(requestHandlerMock.getWebApiResponse(requestMock)).thenThrow(
140-
new SteamApiException(SteamApiException.Cause.MAPPING, Integer
141-
.valueOf(0)));
98+
new SteamApiException(Integer.valueOf(404)));
14299

143100
client.processRequest(requestMock);
144101
}
@@ -147,8 +104,7 @@ public void testProcessExceptionUnexpectedStatusError()
147104
public void testProcessExceptionMessage() throws SteamApiException {
148105

149106
when(requestHandlerMock.getWebApiResponse(requestMock)).thenThrow(
150-
new SteamApiException(SteamApiException.Cause.HTTP_ERROR,
151-
Integer.valueOf(404)));
107+
new SteamApiException(Integer.valueOf(403)));
152108

153109
try {
154110
client.processRequest(requestMock);
@@ -157,7 +113,7 @@ public void testProcessExceptionMessage() throws SteamApiException {
157113
} catch (SteamApiException e) {
158114
assertEquals(
159115
e.getMessage(),
160-
"The Web API request failed (status code: 404).");
116+
"The Web API request failed for security reasons. The supplied Web API key was rejected by Steam. Ensure that the supplied Web API key is valid.");
161117
}
162118
}
163119

0 commit comments

Comments
 (0)