Don't throw exception on HTTP Status Code 5xx?
boekhold opened this issue · comments
I discovered that if I make a request to a server that returns an Internal Server Error (code 500), ws-lite throws an HTTPClientException. That's unexpected to me, because I would expect to just do a switch on resp.statusCode to handle the result of the call (a RESTClient.post() call in this case).
After tracing through the code I do realize that this is caused by URLConnection.getInputStream() throwing an IOException, which in turn I find a strange design decision, but that's not something wslite can do something about.
Can wslite be updated to handle an IOException thrown by URLConnection.getInputStream() in such a way that if it is caused by an actual server response (eg code = 500), this exception is not propagated to client code? Any network related exceptions (invalid IP, timeout etc) should still be treated as exception.
Same goes for HTTP Status Code 4xx.
I integrate with an api that responds with 400 Bad Request, with a JSON body with error details, when I post something the api doesn't understand.
I first expected to get a response from client.post(...)
with response.statusCode
400 and the JSON response in response.text
. But instead I get a RestClientException.
To get the JSON I can do
} catch (RESTClientException e) {
String json = e.response.contentAsString
...
}
But it would be easier if client.post(...)
could return a response.
I'm going through some low-hanging fruit in the open issues, and this is something I could address. But it would be a breaking API change, so it would need discussion.
Does anybody have any arguments in favor of the current behavior, apart from maintaining backwards compatibility?