Skip to content

Commit

Permalink
now httpclient exception doesn't thro nullpointer on missing body
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan committed Oct 26, 2022
1 parent 5129caa commit 6773fc1
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,37 @@ public HttpException(int statusCode, Throwable cause) {
}

/**
* Return the response body content as a bean
* Return the response body content as a bean, or else null if body content doesn't exist.
*
* @param cls The type of bean to convert the response to
* @return The response as a bean
*/
public <T> T bean(Class<T> cls) {
if (httpResponse == null) {
return null;
}
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
return context.readBean(cls, body);
}

/**
* Return the response body content as a UTF8 string.
* Return the response body content as a UTF8 string, or else null if body content doesn't exist.
*/
public String bodyAsString() {
if (httpResponse == null) {
return null;
}
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
return new String(body.content(), StandardCharsets.UTF_8);
}

/**
* Return the response body content as raw bytes.
/**
* Return the response body content as raw bytes, or else null if body content doesn't exist.
*/
public byte[] bodyAsBytes() {
if (httpResponse == null) {
return null;
}
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
return body.content();
}
Expand Down

0 comments on commit 6773fc1

Please sign in to comment.