Skip to content

Commit 3f9a3ab

Browse files
committed
now httpclient exception doesn't thro nullpointer on missing body
1 parent 7ecf327 commit 3f9a3ab

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

client/src/main/java/io/avaje/http/client/HttpException.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,37 @@ public HttpException(int statusCode, Throwable cause) {
9191
}
9292

9393
/**
94-
* Return the response body content as a bean
94+
* Return the response body content as a bean, or else null if body content doesn't exist.
9595
*
9696
* @param cls The type of bean to convert the response to
9797
* @return The response as a bean
9898
*/
9999
public <T> T bean(Class<T> cls) {
100+
if (httpResponse == null) {
101+
return null;
102+
}
100103
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
101104
return context.readBean(cls, body);
102105
}
103106

104107
/**
105-
* Return the response body content as a UTF8 string.
108+
* Return the response body content as a UTF8 string, or else null if body content doesn't exist.
106109
*/
107110
public String bodyAsString() {
111+
if (httpResponse == null) {
112+
return null;
113+
}
108114
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
109115
return new String(body.content(), StandardCharsets.UTF_8);
110116
}
111117

112-
/**
113-
* Return the response body content as raw bytes.
118+
/**
119+
* Return the response body content as raw bytes, or else null if body content doesn't exist.
114120
*/
115121
public byte[] bodyAsBytes() {
122+
if (httpResponse == null) {
123+
return null;
124+
}
116125
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
117126
return body.content();
118127
}

0 commit comments

Comments
 (0)