Uploaded image for project: 'Spring Data REST'
  1. Spring Data REST
  2. DATAREST-388

Can't use controller advices if annotation based event handling is used

    XMLWordPrintable

    Details

      Description

      Spring Data REST is capturing any exceptions thrown by event handler methods and re-throwing them as IllegalStateException. By doing this, Spring Data REST makes it very hard to adopt any exception handling mechanism offered by Spring MVC (controller advices, for example) because we cannot pinpoint which exception we would like to handle in each exception handler of the controller advice (the only exception that we can handle is IllegalStateException).

      The patch in the pull request corrects this behavior. If a event handler throws a unchecked exception, than it makes sure that this very exception is thrown. Otherwise (if it is a checked exception), it resorts to the old behavior, encapsulating the checked exception in a IllegalStateException. Without this patch, the controller advice shown below does not work.

      @ControllerAdvice
      public class ExceptionHandlingControllerAdvice {
        @ExceptionHandler(UnauthorizedException.class)
        public ResponseEntity handleUnauthorizedException(UnauthorizedException exception) {
          String stackTrace = ExceptionUtils.getStackTrace(exception);
          return new ResponseEntity(stackTrace, HttpStatus.FORBIDDEN); 
        }
      }
      

      By applying this patch, I believe Spring Data REST better fits the Spring MVC world.

        Attachments

          Activity

            People

            Assignee:
            olivergierke Oliver Drotbohm
            Reporter:
            saulo2 Saulo Medeiros de Araujo
            Last updater:
            Spring Issues Spring Issues
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 1h
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified