Skip to content

Commit 05c79b8

Browse files
committed
Remove redundant cause attribute from SteamApiException
1 parent ae38456 commit 05c79b8

4 files changed

Lines changed: 24 additions & 119 deletions

File tree

src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiException.java

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,19 @@ public class SteamApiException extends Exception {
66

77
private static final long serialVersionUID = 6414882632273395318L;
88

9-
public enum Cause {
10-
BAD_REQUEST,
11-
FORBIDDEN,
12-
NOT_FOUND,
13-
TOO_MANY_REQUESTS,
14-
INTERNAL_ERROR,
15-
BAD_GATEWAY,
16-
SERVICE_UNAVAILABLE,
17-
MAPPING
18-
}
19-
209
private final String message;
2110
private final Integer statusCode;
2211

23-
public SteamApiException(Cause cause, Throwable exceptionCause) {
24-
12+
public SteamApiException(Throwable exceptionCause) {
2513
super(exceptionCause);
2614

27-
String message = getMessageByCause(cause);
28-
Integer statusCode = getStatusCodeByCause(cause);
29-
30-
this.message = (message != null) ? message : exceptionCause.getMessage();
31-
this.statusCode = (statusCode != null) ? statusCode : HttpStatus.INTERNAL_SERVER_ERROR;
15+
this.message = "The Web API request failed due to an unexpected error: " + exceptionCause.getMessage();
16+
this.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
3217
}
3318

34-
public SteamApiException(Cause cause, Integer statusCode) {
35-
19+
public SteamApiException(int statusCode) {
3620
this.statusCode = statusCode;
37-
38-
String message = getMessageByCause(cause);
39-
40-
if(message != null) {
41-
this.message = message;
42-
} else {
43-
this.message = "The Web API request failed due to an unexpected error.";
44-
}
21+
this.message = getMessageByStatusCode(statusCode);
4522
}
4623

4724
@Override
@@ -57,43 +34,16 @@ public Integer getStatusCode() {
5734
return statusCode;
5835
}
5936

60-
public static Cause getCauseByStatusCode(Integer statusCode) {
61-
62-
if (statusCode == HttpStatus.BAD_REQUEST) { return Cause.BAD_REQUEST; }
63-
else if (statusCode == HttpStatus.FORBIDDEN) { return Cause.FORBIDDEN; }
64-
else if (statusCode == HttpStatus.NOT_FOUND) { return Cause.NOT_FOUND; }
65-
else if (statusCode == HttpStatus.TOO_MANY_REQUESTS) { return Cause.TOO_MANY_REQUESTS; }
66-
else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR) { return Cause.INTERNAL_ERROR; }
67-
else if (statusCode == HttpStatus.BAD_GATEWAY) { return Cause.BAD_GATEWAY; }
68-
else if (statusCode == HttpStatus.SERVICE_UNAVAILABLE) { return Cause.SERVICE_UNAVAILABLE; }
69-
70-
return null;
71-
}
72-
73-
private static Integer getStatusCodeByCause(Cause cause) {
74-
if (cause == Cause.BAD_REQUEST) { return HttpStatus.BAD_REQUEST; }
75-
else if (cause == Cause.FORBIDDEN) { return HttpStatus.FORBIDDEN; }
76-
else if (cause == Cause.NOT_FOUND) { return HttpStatus.NOT_FOUND; }
77-
else if (cause == Cause.TOO_MANY_REQUESTS) { return HttpStatus.TOO_MANY_REQUESTS; }
78-
else if (cause == Cause.INTERNAL_ERROR) { return HttpStatus.INTERNAL_SERVER_ERROR; }
79-
else if (cause == Cause.BAD_GATEWAY) { return HttpStatus.BAD_GATEWAY; }
80-
else if (cause == Cause.SERVICE_UNAVAILABLE) { return HttpStatus.SERVICE_UNAVAILABLE; }
81-
82-
return null;
83-
}
84-
85-
private static String getMessageByCause(Cause cause) {
37+
private static String getMessageByStatusCode(int statusCode) {
8638

87-
if (cause == Cause.BAD_REQUEST) { return "The Web API request failed. Wrong data format was provided."; }
88-
else if (cause == Cause.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."; }
89-
else if (cause == Cause.NOT_FOUND) { return "Failed to found requested resource."; }
90-
else if (cause == Cause.TOO_MANY_REQUESTS) { return "The Web API request rejected. Because too many requests were processed."; }
91-
else if (cause == Cause.INTERNAL_ERROR) { return "The Web API request failed with an internal error."; }
92-
else if (cause == Cause.BAD_GATEWAY) { return "The Web API gateway failed to route request."; }
93-
else if (cause == Cause.SERVICE_UNAVAILABLE) { return "The Web API service unavailable."; }
94-
else if (cause == Cause.MAPPING) {return "The JSON response could not be parsed or mapped to the designated POJO. The most likely cause for this is that"
95-
+ " the Steam API itself changed. Check for newer versions of this library to compensate for this."; }
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."; }
9646

97-
return null;
47+
return "";
9848
}
9949
}

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
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,10 @@ String getWebApiResponse(URI requestUrl) throws SteamApiException {
9797
if (HttpStatus.Is2xxStatus(statusCode)) {
9898
return response.body();
9999
} else{
100-
SteamApiException.Cause cause = SteamApiException.getCauseByStatusCode(statusCode);
101-
throw new SteamApiException(cause, statusCode);
100+
throw new SteamApiException(statusCode);
102101
}
103-
} catch (IOException | InterruptedException e) {
104-
throw new SteamApiException(SteamApiException.Cause.INTERNAL_ERROR, e);
102+
} catch (IOException | InterruptedException exception) {
103+
throw new SteamApiException(exception);
105104
}
106105
}
107106

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.NOT_FOUND,
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.BAD_REQUEST,
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. Wrong data format was provided.");
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)