From 9b6db05e74facae3eeb1e20e4bdbdbe4248bad1a Mon Sep 17 00:00:00 2001 From: Sebastian Simson Date: Thu, 19 May 2022 21:33:41 +0200 Subject: [PATCH] Add not found exception --- .../internal/utils/OkHttpExceptionUtil.java | 28 ++++++++++--------- .../model/CastleApiNotFoundException.java | 10 +++++++ .../io/castle/client/CastleExceptionTest.java | 13 +++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/main/java/io/castle/client/model/CastleApiNotFoundException.java diff --git a/src/main/java/io/castle/client/internal/utils/OkHttpExceptionUtil.java b/src/main/java/io/castle/client/internal/utils/OkHttpExceptionUtil.java index 2bae52e..730002c 100644 --- a/src/main/java/io/castle/client/internal/utils/OkHttpExceptionUtil.java +++ b/src/main/java/io/castle/client/internal/utils/OkHttpExceptionUtil.java @@ -18,20 +18,22 @@ public static CastleRuntimeException handle(IOException e) { public static void handle(Response response) throws CastleServerErrorException { if (!response.isSuccessful() && !response.isRedirect()) { - if (response.code() == 500) { - throw new CastleApiInternalServerErrorException(response); - } - if (response.code() == 422) { - try { - String body = response.peekBody(Long.MAX_VALUE).string(); - JsonObject json = (JsonObject) JsonParser.parseString(body); - String type = json.get("type").getAsString(); + switch (response.code()) { + case 500: + throw new CastleApiInternalServerErrorException(response); + case 422: + try { + String body = response.peekBody(Long.MAX_VALUE).string(); + JsonObject json = (JsonObject) JsonParser.parseString(body); + String type = json.get("type").getAsString(); - if (type.equals("invalid_request_token")) { - throw new CastleApiInvalidRequestTokenException(response); - } - } catch (IOException | JsonSyntaxException | JsonIOException | IllegalStateException ignored) {} - throw new CastleApiInvalidParametersException(response); + if (type.equals("invalid_request_token")) { + throw new CastleApiInvalidRequestTokenException(response); + } + } catch (IOException | JsonSyntaxException | JsonIOException | IllegalStateException ignored) {} + throw new CastleApiInvalidParametersException(response); + case 404: + throw new CastleApiNotFoundException(response); } throw new CastleServerErrorException(response); } diff --git a/src/main/java/io/castle/client/model/CastleApiNotFoundException.java b/src/main/java/io/castle/client/model/CastleApiNotFoundException.java new file mode 100644 index 0000000..7694096 --- /dev/null +++ b/src/main/java/io/castle/client/model/CastleApiNotFoundException.java @@ -0,0 +1,10 @@ +package io.castle.client.model; + +import io.castle.client.model.CastleServerErrorException; +import okhttp3.Response; + +public class CastleApiNotFoundException extends CastleServerErrorException { + public CastleApiNotFoundException(Response response) { + super(response); + } +} diff --git a/src/test/java/io/castle/client/CastleExceptionTest.java b/src/test/java/io/castle/client/CastleExceptionTest.java index 58bab88..04665ec 100644 --- a/src/test/java/io/castle/client/CastleExceptionTest.java +++ b/src/test/java/io/castle/client/CastleExceptionTest.java @@ -119,4 +119,17 @@ public void invalidJsonError() { OkHttpExceptionUtil.handle(response); } + + @Test(expected = CastleApiNotFoundException.class) + public void notFoundError() { + //Given + Response response = new Response.Builder() + .code(404) + .request(new Request.Builder().url("http://localhost").build()) + .protocol(Protocol.HTTP_1_1) + .message("Message") + .build(); + + OkHttpExceptionUtil.handle(response); + } }