Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.6k stars 594 forks source link

Jackson parser exceptions are not exposed through getParsingError() #451

Closed tokarenko closed 1 year ago

tokarenko commented 2 years ago

Describe the bug getParsingError() is null when Jackson parser fails at least for the following errors:

To Reproduce Steps to reproduce the behavior: In the below code failureMessage does not contain parsing error even if Jackson parser fails.

Unirest.config().setObjectMapper(new JacksonObjectMapper());

Book book = Unirest.get("https://...")
                      .asObject(Book.class)
                      .ifFailure(String.class, r -> 
                              {
                                failureMessage = "HTTP request failed."
                                + System.lineSeparator() + "Status code: " + r.getStatus()
                                + System.lineSeparator() + "Body: " + r.getBody().substring(0,50);
                                r.getParsingError().ifPresent(e -> {
                                  failureMessage += System.lineSeparator() + "Parsing error: " + e.getMessage();
                                });
                              }
                            )
                       .getBody();

I was able to retreive errors only via direct call to com.fasterxml.jackson.databind.ObjectMapper:

Book book = mapper.readValue(Unirest.get("https://...").asString().getBody(), Book.class);

Expected behavior getParsingError() returns UnirestParsingException when Jackson parser fails

Screenshots N/A

Environmental Data:

Additional context Add any other context about the problem here.

ryber commented 2 years ago

Ah, this is because you have performed another mapping operation with .ifFailure(String.class, r -> {}), so the parsing to String worked fine, I totally get why you want the original error though. Let me think about that

If you had just used ifFailure(r -> {}) without the extra mapping, then you would see the error and the original body is also on the parsingException

ryber commented 1 year ago

this is complete in 3.14.0 / 4.0.0-RC6. Note that if the 2nd mapping operation fails due to a parsing error you will get THAT error and not the previous one.