From 723ce82599bf9316d9c5346fa7ad13c5efc8538d Mon Sep 17 00:00:00 2001 From: lpradel Date: Sun, 29 Jun 2025 19:18:29 +0200 Subject: [PATCH] Simplify exception handling to prepare for exposing HTTP status code --- .../core/exception/SteamApiException.java | 10 +----- .../core/exception/SteamApiKeyException.java | 10 ------ .../request/SteamWebApiRequestHandler.java | 19 ++++++----- .../SteamWebApiRequestHandlerTest.java | 32 +++++++++---------- 4 files changed, 26 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiKeyException.java diff --git a/src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiException.java b/src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiException.java index 86f6574..adfcd40 100644 --- a/src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiException.java +++ b/src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiException.java @@ -8,15 +8,7 @@ public enum Cause { HTTP_ERROR, FORBIDDEN, INTERNAL_ERROR, MAPPING } - private String message; - - public SteamApiException(String message) { - super(message); - } - - public SteamApiException(String message, Throwable cause) { - super(message, cause); - } + private final String message; public SteamApiException(Cause cause, Throwable exceptionCause) { diff --git a/src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiKeyException.java b/src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiKeyException.java deleted file mode 100644 index b8c2e82..0000000 --- a/src/main/java/com/lukaspradel/steamapi/core/exception/SteamApiKeyException.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.lukaspradel.steamapi.core.exception; - -public class SteamApiKeyException extends SteamApiException { - - private static final long serialVersionUID = 5154813503049374498L; - - public SteamApiKeyException(String message) { - super(message); - } -} diff --git a/src/main/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandler.java b/src/main/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandler.java index 8bfb55f..957f47a 100644 --- a/src/main/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandler.java +++ b/src/main/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandler.java @@ -2,7 +2,6 @@ import com.lukaspradel.steamapi.core.SteamApiRequestHandler; import com.lukaspradel.steamapi.core.exception.SteamApiException; -import com.lukaspradel.steamapi.core.exception.SteamApiKeyException; import java.io.IOException; import java.net.URI; @@ -14,6 +13,7 @@ import java.util.Map; import java.util.stream.Stream; +import static com.lukaspradel.steamapi.core.exception.SteamApiException.Cause.INTERNAL_ERROR; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.joining; @@ -29,7 +29,7 @@ public String getWebApiResponse(SteamWebApiRequest request) throws SteamApiExcep return getWebApiResponse(getRequestUrl(request)); } - URI getRequestUrl(SteamWebApiRequest request) throws SteamApiException { + URI getRequestUrl(SteamWebApiRequest request) { String scheme = getProtocol(); String host = request.getBaseUrl(); String path = getRequestPath(request); @@ -49,14 +49,14 @@ String getRequestPath(SteamWebApiRequest request) { .collect(joining("/", "/", "")); } - String getRequestQuery(Map parameters) throws SteamApiException { + String getRequestQuery(Map parameters) { if (getKey() == null) { - throw new SteamApiKeyException("Steam API key is not present or null"); + throw new IllegalArgumentException("Steam API key is not present or null"); } for (var e : parameters.entrySet()) { if (e.getKey() == null) { - throw new SteamApiException("The key of the parameter with the value '" + e.getValue() + "' is null"); + throw new IllegalArgumentException("The key of the parameter with the value '" + e.getValue() + "' is null"); } } @@ -69,7 +69,7 @@ String getRequestQuery(Map parameters) throws SteamApiException .collect(joining("&")); } - URI getRequestUri(String scheme, String host, String path, String query) throws SteamApiException { + URI getRequestUri(String scheme, String host, String path, String query) { var uri = new StringBuilder(); uri.append(scheme); @@ -82,8 +82,8 @@ URI getRequestUri(String scheme, String host, String path, String query) throws try { return new URI(uri.toString()); } catch (URISyntaxException e) { - throw new SteamApiException( - "Failed to process the Web API request due to the following error: " + e.getMessage(), e); + throw new IllegalArgumentException( + "Failed to construct a valid request URI due to the following error: " + e.getMessage(), e); } } @@ -103,8 +103,7 @@ String getWebApiResponse(URI requestUrl) throws SteamApiException { throw new SteamApiException(SteamApiException.Cause.HTTP_ERROR, statusCode); } } catch (IOException | InterruptedException e) { - throw new SteamApiException( - "The Web API request failed due to the following error: " + e.getMessage(), e); + throw new SteamApiException(INTERNAL_ERROR, e); } } diff --git a/src/test/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandlerTest.java b/src/test/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandlerTest.java index 1395e39..b1d1570 100644 --- a/src/test/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandlerTest.java +++ b/src/test/java/com/lukaspradel/steamapi/webapi/request/SteamWebApiRequestHandlerTest.java @@ -1,8 +1,7 @@ package com.lukaspradel.steamapi.webapi.request; -import com.lukaspradel.steamapi.webapi.core.BaseTest; import com.lukaspradel.steamapi.core.exception.SteamApiException; -import com.lukaspradel.steamapi.core.exception.SteamApiKeyException; +import com.lukaspradel.steamapi.webapi.core.BaseTest; import com.lukaspradel.steamapi.webapi.core.SteamWebApiInterface; import com.lukaspradel.steamapi.webapi.core.SteamWebApiInterfaceMethod; import com.lukaspradel.steamapi.webapi.core.SteamWebApiVersion; @@ -19,6 +18,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; @@ -34,14 +34,14 @@ public class SteamWebApiRequestHandlerTest extends BaseTest { private static final int UNAUTHORIZED = 401; private static final int INTERNAL_SERVER_ERROR = 500; - private String key = "12345"; + private final String key = "12345"; - private URI uri = URI.create("http://localhost:80"); + private final URI uri = URI.create("http://localhost:80"); - private SteamWebApiRequestHandler requestHandlerHttps = new SteamWebApiRequestHandler( + private final SteamWebApiRequestHandler requestHandlerHttps = new SteamWebApiRequestHandler( true, key); - private SteamWebApiRequestHandler requestHandlerHttpsSpy = spy(requestHandlerHttps); + private final SteamWebApiRequestHandler requestHandlerHttpsSpy = spy(requestHandlerHttps); @Mock private SteamWebApiRequest requestMock; @@ -52,10 +52,10 @@ public class SteamWebApiRequestHandlerTest extends BaseTest { @Mock private HttpResponse httpResponseMock; - private ArgumentMatcher> bodyHandlerMatcher = arg -> arg != null ? true : false; + private final ArgumentMatcher> bodyHandlerMatcher = Objects::nonNull; @Test - public void testGetRequestUrl() throws SteamApiException { + public void testGetRequestUrl() { Map parameters = new HashMap(); @@ -83,7 +83,7 @@ public void testGetRequestUrl() throws SteamApiException { } @Test - public void testGetRequestQuery() throws SteamApiException { + public void testGetRequestQuery() { var parameters = new LinkedHashMap(); parameters.put("key", null); // prepopulate the key, it gets set in the getRequestQuery method parameters.put("test-parameter", "test-value"); @@ -95,20 +95,20 @@ public void testGetRequestQuery() throws SteamApiException { assertEquals(query, "key=12345&test-parameter=test-value&format=json&input_json=%7B%22steamid%22%3A%2276561198039505218%22%7D"); } - @Test(expectedExceptions = SteamApiKeyException.class) + @Test(expectedExceptions = IllegalArgumentException.class) public void testGetRequestQueryApiKeyIsNull() throws SteamApiException { var reqHandler = new SteamWebApiRequestHandler(true, null); reqHandler.getRequestQuery(null); } - @Test(expectedExceptions = SteamApiException.class) - public void testGetRequestQueryParameterKeyIsNull() throws SteamApiException { + @Test(expectedExceptions = IllegalArgumentException.class) + public void testGetRequestQueryParameterKeyIsNull() { var params = new HashMap(); params.put(null, "test-value"); try { requestHandlerHttps.getRequestQuery(params); - } catch (SteamApiException e) { + } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "The key of the parameter with the value 'test-value' is null"); throw e; } @@ -130,7 +130,7 @@ public void testGetRequestPath() { } @Test - public void testGetRequestUri() throws SteamApiException { + public void testGetRequestUri() { String scheme = "https"; String host = "api.steampowered.com"; String path = "/IPlayerService/GetOwnedGames/v0001"; @@ -152,8 +152,8 @@ public void testGetRequestUri() throws SteamApiException { "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001?key=12345&format=json&input_json=%7B%22steamid%22%3A%2276561198039505218%22%7D"); } - @Test(expectedExceptions = SteamApiException.class) - public void testGetRequestUriWithInvalidUri() throws SteamApiException { + @Test(expectedExceptions = IllegalArgumentException.class) + public void testGetRequestUriWithInvalidUri() { String scheme = ""; String host = "api.steampowered.com"; String path = "/IPlayerService/GetOwnedGames/v0001";