Skip to content

Commit

Permalink
Merge pull request #99 from JPCanaverde/fix-npe-98
Browse files Browse the repository at this point in the history
Do not append error detail to message when it does not exist
  • Loading branch information
tsolakoua authored Apr 23, 2021
2 parents e8109c2 + 9863600 commit 3871908
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/amadeus/exceptions/ResponseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/amadeus/ExceptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 3871908

Please sign in to comment.