Skip to content

Commit

Permalink
issue #451 pass original failure reason to new mapped response in ifF…
Browse files Browse the repository at this point in the history
…ailure
  • Loading branch information
ryber committed Nov 26, 2022
1 parent e1c0744 commit afa69a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@
class ErrorParsingTest extends BddTest {
private boolean errorCalled;

@Test
void passParsingErrorsOnInFalure() {
MockServer.setStringResponse("not json");

Unirest.get(MockServer.GET)
.asObject(RequestCapture.class)
.ifFailure(String.class, r ->
{
assertTrue(r.getParsingError().isPresent());
assertTrue(r.getParsingError().get().getMessage().contains("jackson"));
assertEquals("not json", r.getParsingError().get().getOriginalBody());
}
)
.ifSuccess(s -> fail("no"));
}

@Test
void parsingAnAlternativeErrorObject() {
MockServer.setJsonAsResponse(new ErrorThing("boom!"));
Expand Down
4 changes: 3 additions & 1 deletion unirest/src/main/java/kong/unirest/core/BaseResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ private String getErrorBody() {
public <E> HttpResponse<T> ifFailure(Class<? extends E> errorClass, Consumer<HttpResponse<E>> consumer) {
if (!isSuccess()) {
E error = mapError(errorClass);
consumer.accept(new BasicResponse(this, error));
BasicResponse br = new BasicResponse(this, error);
getParsingError().ifPresent(p -> br.setParsingException(p.getOriginalBody(), p));
consumer.accept(br);
}
return this;
}
Expand Down

0 comments on commit afa69a4

Please sign in to comment.