Skip to content

Commit d5b689b

Browse files
Add not found exception (#118)
1 parent 93833d8 commit d5b689b

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

src/main/java/io/castle/client/internal/utils/OkHttpExceptionUtil.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ public static CastleRuntimeException handle(IOException e) {
1818

1919
public static void handle(Response response) throws CastleServerErrorException {
2020
if (!response.isSuccessful() && !response.isRedirect()) {
21-
if (response.code() == 500) {
22-
throw new CastleApiInternalServerErrorException(response);
23-
}
24-
if (response.code() == 422) {
25-
try {
26-
String body = response.peekBody(Long.MAX_VALUE).string();
27-
JsonObject json = (JsonObject) JsonParser.parseString(body);
28-
String type = json.get("type").getAsString();
21+
switch (response.code()) {
22+
case 500:
23+
throw new CastleApiInternalServerErrorException(response);
24+
case 422:
25+
try {
26+
String body = response.peekBody(Long.MAX_VALUE).string();
27+
JsonObject json = (JsonObject) JsonParser.parseString(body);
28+
String type = json.get("type").getAsString();
2929

30-
if (type.equals("invalid_request_token")) {
31-
throw new CastleApiInvalidRequestTokenException(response);
32-
}
33-
} catch (IOException | JsonSyntaxException | JsonIOException | IllegalStateException ignored) {}
34-
throw new CastleApiInvalidParametersException(response);
30+
if (type.equals("invalid_request_token")) {
31+
throw new CastleApiInvalidRequestTokenException(response);
32+
}
33+
} catch (IOException | JsonSyntaxException | JsonIOException | IllegalStateException ignored) {}
34+
throw new CastleApiInvalidParametersException(response);
35+
case 404:
36+
throw new CastleApiNotFoundException(response);
3537
}
3638
throw new CastleServerErrorException(response);
3739
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.castle.client.model;
2+
3+
import io.castle.client.model.CastleServerErrorException;
4+
import okhttp3.Response;
5+
6+
public class CastleApiNotFoundException extends CastleServerErrorException {
7+
public CastleApiNotFoundException(Response response) {
8+
super(response);
9+
}
10+
}

src/test/java/io/castle/client/CastleExceptionTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,17 @@ public void invalidJsonError() {
119119

120120
OkHttpExceptionUtil.handle(response);
121121
}
122+
123+
@Test(expected = CastleApiNotFoundException.class)
124+
public void notFoundError() {
125+
//Given
126+
Response response = new Response.Builder()
127+
.code(404)
128+
.request(new Request.Builder().url("http://localhost").build())
129+
.protocol(Protocol.HTTP_1_1)
130+
.message("Message")
131+
.build();
132+
133+
OkHttpExceptionUtil.handle(response);
134+
}
122135
}

0 commit comments

Comments
 (0)