Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 20c688f

Browse files
authored
Merge pull request #31 from Sybit-Education/develop
simplify/streamline exception handling
2 parents 289b9de + 8f34c4b commit 20c688f

File tree

4 files changed

+55
-25
lines changed

4 files changed

+55
-25
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ src/itest/resources/credentials.properties
2020
# IDE
2121
.idea
2222
src/itest/resources/credentials.properties
23+
out/
24+
*.iml

src/itest/java/com/sybit/airtable/TableSelectJacksonOMTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package com.sybit.airtable;
88

99
import com.fasterxml.jackson.core.JsonProcessingException;
10+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
1011
import com.mashape.unirest.http.ObjectMapper;
1112
import com.sybit.airtable.exception.AirtableException;
1213
import com.sybit.airtable.movies.Movie;
@@ -36,6 +37,15 @@ public void setup() throws AirtableException {
3637
public <T> T readValue(final String value, final Class<T> valueType) {
3738
try {
3839
return objectMapper.readValue(value, valueType);
40+
} catch (UnrecognizedPropertyException e) {
41+
try {
42+
// dummy instance to follow code flow and execute HttpResponseExceptionHandler.onResponse
43+
T instance = valueType.newInstance();
44+
return instance;
45+
}
46+
catch (IllegalAccessException | InstantiationException e1) {
47+
throw new RuntimeException(e);
48+
}
3949
} catch (IOException e) {
4050
throw new RuntimeException(e);
4151
}
@@ -120,4 +130,16 @@ public void testSelectNonExistingTable() throws AirtableException, HttpResponseE
120130
}
121131

122132

133+
@Test
134+
public void testSelectNonExistingExceptionMessageTable() throws HttpResponseException {
135+
136+
String message;
137+
try {
138+
base.table("NotExists", Movie.class).select();
139+
message = "Success";
140+
} catch (AirtableException e) {
141+
message = e.getMessage();
142+
}
143+
assertEquals("Could not find table NotExists in application " + base.name() + " (TABLE_NOT_FOUND) [Http code 404]", message);
144+
}
123145
}

src/itest/java/com/sybit/airtable/TableSelectTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ public void testSelectNonExistingTable() throws AirtableException, HttpResponseE
7979
assertNotNull(retval);
8080
}
8181

82+
@Test
83+
public void testSelectNonExistingTableExceptionMessage() throws HttpResponseException {
84+
85+
String message;
86+
try {
87+
base.table("NotExists", Movie.class).select();
88+
message = "Success";
89+
} catch (AirtableException e) {
90+
message = e.getMessage();
91+
}
92+
assertEquals("Could not find table NotExists in application " + base.name() + " (TABLE_NOT_FOUND) [Http code 404]", message);
93+
}
94+
8295
// @Test
8396
// public void testSelectWithSerializedNames() throws AirtableException, HttpResponseException {
8497
//

src/main/java/com/sybit/airtable/exception/HttpResponseExceptionHandler.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.mashape.unirest.http.HttpResponse;
1111
import com.sybit.airtable.vo.Error;
1212

13+
import java.util.Map;
14+
1315
/**
1416
* Handle HTTP responses and create exceptions.
1517
*
@@ -22,36 +24,27 @@ public static void onResponse(HttpResponse response) throws AirtableException {
2224
final Integer statusCode = response.getStatus();
2325
String message = convertStreamToString(response.getRawBody());
2426

25-
final Gson gson = new Gson();
26-
Error err = gson.fromJson(message, Error.class);
27-
switch (statusCode) {
28-
case 401:
29-
throw new AirtableException("AUTHENTICATION_REQUIRED", "You should provide valid api key to perform this operation", statusCode);
30-
case 403:
31-
throw new AirtableException("NOT_AUTHORIZED", "You are not authorized to perform this operation", statusCode);
32-
33-
case 404:
34-
message = ( err.getMessage() != null) ? err.getMessage() : "Could not find what you are looking for";
35-
throw new AirtableException("NOT_FOUND", message, statusCode);
36-
37-
case 413:
38-
throw new AirtableException("REQUEST_TOO_LARGE", "Request body is too large", statusCode);
27+
Error err = extractError(message);
3928

40-
case 422:
41-
throw new AirtableException(err.getType(), err.getMessage(), statusCode);
42-
43-
case 429:
44-
throw new AirtableException("TOO_MANY_REQUESTS", "You have made too many requests in a short period of time. Please retry your request later", statusCode);
29+
throw new AirtableException(err.getType(), err.getMessage(), statusCode);
30+
}
4531

46-
case 500:
47-
throw new AirtableException("SERVER_ERROR", "Try again. If the problem persists, contact support.", statusCode);
32+
private static Error extractError(String message) {
4833

49-
case 503:
50-
throw new AirtableException("SERVICE_UNAVAILABLE", "The service is temporarily unavailable. Please retry shortly.", statusCode);
34+
final Gson gson = new Gson();
5135

52-
default:
53-
throw new AirtableException("UNDEFINED_ERROR", message, statusCode);
36+
Error err;
37+
try {
38+
Map<String, Object> jsonMap = gson.fromJson(message, Map.class);
39+
String innerJson = gson.toJson(jsonMap.get("error"));
40+
err = gson.fromJson(innerJson, Error.class);
41+
} catch (Exception ignored) {
42+
err = new Error();
43+
err.setType("UNDEFINED_ERROR");
44+
err.setMessage(message);
5445
}
46+
47+
return err;
5548
}
5649

5750
public static String convertStreamToString(java.io.InputStream is) {

0 commit comments

Comments
 (0)