Skip to content

Commit

Permalink
enable mapping the error into a string
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Mar 30, 2020
1 parent 0673632 commit e58e132
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions unirest/src/main/java/kong/unirest/BaseResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public HttpResponse<T> ifFailure(Consumer<HttpResponse<T>> consumer) {
public <E> E mapError(Class<? extends E> errorClass) {
if (!isSuccess()) {
String errorBody = getErrorBody();
if(String.class.equals(errorClass)){
return (E) errorBody;
}
try {
return config.getObjectMapper().readValue(errorBody, errorClass);
} catch (RuntimeException e) {
Expand Down
11 changes: 11 additions & 0 deletions unirest/src/test/java/BehaviorTests/ErrorParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public void parsingAnAlternativeErrorObject() {
assertErrorThing(e);
}

@Test
public void mapTheErrorToAString() {
MockServer.setJsonAsResponse(new ErrorThing("boom!"));

String e = Unirest.get(MockServer.ERROR_RESPONSE)
.asObject(RequestCapture.class)
.mapError(String.class);

assertEquals("{\"message\":\"boom!\"}", e);
}

@Test
public void parsingAnAlternativeErrorObject_StringBody() {
MockServer.setJsonAsResponse(new ErrorThing("boom!"));
Expand Down

0 comments on commit e58e132

Please sign in to comment.