Skip to content

Exception hierarchy under HttpClientException and HttpServerException for the RestTemplate [SPR-15404] #19967

Closed

Description

@spring-projects-issues

Jerzy Krolak opened SPR-15404 and commented

The default RestTemplate error handler throws HttpClientErrorException or HttpServerErrorException, 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 and HttpServerErrorException could simplify quite a lot of my code.

Sample code, actual:

try {
    Book book = restTemplate.getForObject("http://example.com/books/1", Book.class);
    return book;
} catch (HttpClientErrorException e) {
    if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
        // handle the 404 error
    }
    throw e; // can't do anything about other http errors, anyway
}

Sample code, possible:

try {
    Book book = restTemplate.getForObject("http://example.com/books/1", Book.class);
    return book;
} catch (HttpNotFoundErrorException e) {
        // handle the 404 error
}

This change should be pretty easy to introduce, and by subclassing HttpClientErrorException and HttpServerErrorException should be backward compatible.

I could prepare an appropriate pull request, if you like the general idea.


Affects: 5.0.8

Issue Links:

Referenced from: commits 7f0e348, 2216964

Activity

  1. spring-projects-issues commented on Jul 2, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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.

  2. spring-projects-issues commented on Jul 2, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    On a first glance it seems okay to me. Arjen Poutsma or Juergen Hoeller?

    The implementation creates a package cycle where .client depends on .client.response and vice-versa. A sub-class of DefaultResponseErrorHandler together with the exceptions could break the cycle. Or the exceptions would move up but they're too many.

    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
    }
  3. spring-projects-issues commented on Jul 3, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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.

  4. spring-projects-issues commented on Jul 3, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Jerzy Krolak commented

    Thanks for your feedback - I'll update the code tomorrow. Meanwhile, a few more questions:

    • I added just most common HTTP errors. What other exceptions you'd like to see?
    • what javadoc / constructors should I add - same as in HttpClientErrorException?

    and one more general:

    • have you considered adding a preview of the response body in the exception message? So that, instead of this:
    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]
  5. spring-projects-issues commented on Jul 4, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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?

  6. spring-projects-issues commented on Aug 6, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Jerzy Krolak commented

    Thank you!

  7. added
    type: enhancement
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions