New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exception hierarchy under HttpClientException and HttpServerException for the RestTemplate [SPR-15404] #19967
Comments
Jerzy Krolak commented I know it's been over a year, but I still see a point in implementing this feature. I've written a piece of code to illustrate my idea - maybe you could have a look at that: https://github.com/jerzykrlk/spring-framework/pull/1/files I can polish it up later. |
Rossen Stoyanchev commented On a first glance it seems okay to me. Arjen Poutsma or Juergen Hoeller? The implementation creates a package cycle where Another idea is to have them nested in either HttpClientErrorException or HttpServerErrorException with a slightly abbreviated name so it becomes: } catch (HttpClientErrorException.BadRequest ex) {
// handle the 400
} or: } catch (HttpClientErrorException.BadRequest400 ex) {
// handle the 400
} |
Arjen Poutsma commented Looks good to me as well. I like Rossen's idea of nesting the exceptions in their "parent", as I don't think we should introduce so many new public types (and files!) this late in RestTemplate's lifecycle. |
Jerzy Krolak commented Thanks for your feedback - I'll update the code tomorrow. Meanwhile, a few more questions:
and one more general:
org.springframework.web.client.HttpClientErrorException.BadRequest: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] ... I could see this: org.springframework.web.client.HttpClientErrorException.BadRequest: 400 Bad Request [<html><head><title>page not found</title>...]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] or even nicer... org.springframework.web.client.HttpClientErrorException.Conflict: 409 Conflict [{
"error": {
"errors": [
{
"domain": "global",
"reason": "conflict",
"message": "You already own this bucket. Please select another name."
}
],
"code": 409,
"message": "You already own this bucket. Please select another name."
}
}]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] |
Jerzy Krolak commented Hi. I've updated the code to static inner class exceptions - see https://github.com/jerzykrlk/spring-framework/pull/1/files - can you have a look now? |
Jerzy Krolak commented Thank you! |
Jerzy Krolak opened SPR-15404 and commented
The default
RestTemplate
error handler throwsHttpClientErrorException
orHttpServerErrorException
, which are quite general, compared to the variety of HTTP error codes and their REST semantics.In a heavily rest-based application, introducing subclasses of
HttpClientErrorException
andHttpServerErrorException
could simplify quite a lot of my code.Sample code, actual:
Sample code, possible:
This change should be pretty easy to introduce, and by subclassing
HttpClientErrorException
andHttpServerErrorException
should be backward compatible.I could prepare an appropriate pull request, if you like the general idea.
Affects: 5.0.8
Issue Links:
HttpStatusCodeException
with empty status text #22162 HTTP error 404 from spring web causes "404 null" message in RestTemplateReferenced from: commits 7f0e348, 2216964
The text was updated successfully, but these errors were encountered: