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 2d437ea commit 82ca2af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 3.14.0
* issue #461 return HttpResponse<Empty> for asEmpty()
* issue #450 add authenticated proxies to request
* issue #451 pass original failure reason to new mapped response in ifFailure

## 3.13.13
* Cookie dates always follow US Locale to avoid invalid unicode in headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.eclipse.jetty.server.Request;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -39,6 +40,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 -> {throw new RuntimeException("no");});
}

@Test
void parsingAnAlternativeErrorObject() {
MockServer.setJsonAsResponse(new ErrorThing("boom!"));
Expand Down
4 changes: 3 additions & 1 deletion unirest/src/main/java/kong/unirest/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 82ca2af

Please sign in to comment.