Spring Framework
  1. Spring Framework
  2. SPR-9283

Improved support for REST API error reporting

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Complete
    • Affects Version/s: 3.1.1
    • Fix Version/s: 3.2 M2
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      When implementing a REST API, Spring MVC provides full control over what ResponseEntity is returned to a client, which includes full support for setting custom HTTP status codes, response headers, and the response body. This is great and very flexible; however, the error reporting support in Spring MVC for common cases could be made more convenient.

      Specifically, there is no way to auto-map an application business exception to a error response that contains all of the following bits of information: 1. an appropriate HTTP status code, and 2. a custom ErrorBody consisting of an application-specific error message and code. Yes, I know you can annotate an Exception with @ResponseStatus to specify which HTTP status code should be returned when that Exception is thrown. However, there is no ability to specify error data that should be returned in the body (message, code, etc) along with that. You can set a "reason" String, but unfortunately that is just passed off to HttpServletResponse#sendError(int, String) which isn't easily customized & by default generates a HTML response (not what you want with machine-to-machine communication--you typically want a JSON error body).

      Facebook is an example of a REST API that returns a error body along with appropriate HTTP status codes in their API error responses e.g.:

      https://graph.facebook.com/me
      {
         "error": {
            "message": "An active access token must be used to query information about the current user.",
            "type": "OAuthException",
            "code": 2500
         }
      }
      
      {
        "error": {
          "message": "(#803) Some of the aliases you requested do not exist: whatever", 
          "type": "OAuthException", 
          "code": 803
        }
      }
      

      It would be useful if Spring MVC had a documented way of throwing a business exception that automatically resulted in a error response with an appropriate HTTP status code plus a application/json response body containing a application-specific error message and error code.

      Related, if a @Valid object validation failure could also be auto-mapped to an appropriate error structure (e.g. a 400 error status plus a body containing the field(s) in error along with messages and codes for each field error), that would be quite useful as well.

        Issue Links

          Activity

          Hide
          Rossen Stoyanchev added a comment - - edited

          SPR-9290 provides guidance on customizing the Servlet container error page, for cases when HttpServletResponse.sendError(int, String) is used (e.g. @ResponseStatus). SPR-9310 adds automatic handling for BindException.

          There is also a new ResponseEntityExceptionHandler base class with an @ExceptionHandler method that handles standard Spring MVC exceptions. It's functionally equivalent to the DefaultHandlerExceptionResolver but returns a ResponseEntity (instead of a ModelAndView) and relies on message converters.

          Show
          Rossen Stoyanchev added a comment - - edited SPR-9290 provides guidance on customizing the Servlet container error page, for cases when HttpServletResponse.sendError(int, String) is used (e.g. @ResponseStatus ). SPR-9310 adds automatic handling for BindException . There is also a new ResponseEntityExceptionHandler base class with an @ExceptionHandler method that handles standard Spring MVC exceptions. It's functionally equivalent to the DefaultHandlerExceptionResolver but returns a ResponseEntity (instead of a ModelAndView) and relies on message converters.
          Hide
          Pavel Orehov added a comment -

          I'm trying to use that new feature but things does not work as expected.
          http://forum.springsource.org/showthread.php?131688-Improved-support-for-REST-API-error-reporting-in-Spring-3-2-0-M2
          Would appreciate any help.

          Show
          Pavel Orehov added a comment - I'm trying to use that new feature but things does not work as expected. http://forum.springsource.org/showthread.php?131688-Improved-support-for-REST-API-error-reporting-in-Spring-3-2-0-M2 Would appreciate any help.
          Hide
          Pavel Orehov added a comment -

          It would be very helpful if I could also bind MethodHandler in exception handler method with @ExceptionHandler annotation.

          Like the following code:

          @ExceptionHandler(Exception.class)
          public ResponseEntity<Object> handleException(Exception ex, WebRequest request, MethodHandler handler)

          { ... }
          Show
          Pavel Orehov added a comment - It would be very helpful if I could also bind MethodHandler in exception handler method with @ExceptionHandler annotation. Like the following code: @ExceptionHandler(Exception.class) public ResponseEntity<Object> handleException(Exception ex, WebRequest request, MethodHandler handler) { ... }

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              Keith Donald
              Last updater:
              Pavel Orehov
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 21 weeks, 2 days ago