diff --git a/src/main/java/com/amadeus/exceptions/ResponseException.java b/src/main/java/com/amadeus/exceptions/ResponseException.java index 9ac7784b..1614e8c4 100644 --- a/src/main/java/com/amadeus/exceptions/ResponseException.java +++ b/src/main/java/com/amadeus/exceptions/ResponseException.java @@ -90,7 +90,9 @@ private static StringBuffer getErrorsDescription(Response response) { message.append(String.format("[%s] ", source.get("parameter").getAsString())); } } - message.append(String.format("%s", json.get("detail").getAsString())); + if (json.has("detail") && !json.get("detail").isJsonNull()) { + message.append(String.format("%s", json.get("detail").getAsString())); + } } return message; } diff --git a/src/test/java/com/amadeus/ExceptionsTest.java b/src/test/java/com/amadeus/ExceptionsTest.java index cf363291..edbebf09 100644 --- a/src/test/java/com/amadeus/ExceptionsTest.java +++ b/src/test/java/com/amadeus/ExceptionsTest.java @@ -152,4 +152,17 @@ public class ExceptionsTest { assertTrue(new ParserException(null) instanceof ResponseException); assertTrue(new ServerException(null) instanceof ResponseException); } + + @Test public void testForErrorsWithoutDetail() { + Response response = mock(Response.class); + when(response.getStatusCode()).thenReturn(401); + String body = "{\"errors\":[{}]}"; + JsonObject json = new JsonParser().parse(body).getAsJsonObject(); + when(response.getResult()).thenReturn(json); + when(response.isParsed()).thenReturn(true); + + ResponseException error = new ResponseException(response); + assertEquals(error.toString(), "com.amadeus.exceptions.ResponseException: [401]" + + "\n"); + } }