Skip to content

Commit 9863600

Browse files
committed
Do not append error detail to message when it does not exist
1 parent e8109c2 commit 9863600

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/main/java/com/amadeus/exceptions/ResponseException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ private static StringBuffer getErrorsDescription(Response response) {
9090
message.append(String.format("[%s] ", source.get("parameter").getAsString()));
9191
}
9292
}
93-
message.append(String.format("%s", json.get("detail").getAsString()));
93+
if (json.has("detail") && !json.get("detail").isJsonNull()) {
94+
message.append(String.format("%s", json.get("detail").getAsString()));
95+
}
9496
}
9597
return message;
9698
}

src/test/java/com/amadeus/ExceptionsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,17 @@ public class ExceptionsTest {
152152
assertTrue(new ParserException(null) instanceof ResponseException);
153153
assertTrue(new ServerException(null) instanceof ResponseException);
154154
}
155+
156+
@Test public void testForErrorsWithoutDetail() {
157+
Response response = mock(Response.class);
158+
when(response.getStatusCode()).thenReturn(401);
159+
String body = "{\"errors\":[{}]}";
160+
JsonObject json = new JsonParser().parse(body).getAsJsonObject();
161+
when(response.getResult()).thenReturn(json);
162+
when(response.isParsed()).thenReturn(true);
163+
164+
ResponseException error = new ResponseException(response);
165+
assertEquals(error.toString(), "com.amadeus.exceptions.ResponseException: [401]"
166+
+ "\n");
167+
}
155168
}

0 commit comments

Comments
 (0)