0

I have my Spring error controller, and I need to get the actual exception class so I can print stack trace and other things like that. This is my error controller

@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {

    @RequestMapping("/error")
    public String handleError() {
        return "somethingwentwrong";
    }

    @Override
    public String getErrorPath() {
        return null;
    }
}

I know its not much, but I need the exception object to be able to do some extra handling.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
BeABraveDude
  • 45
  • 1
  • 4
  • Have you inspected the code in `BasicErrorController`? – chrylis -cautiouslyoptimistic- Apr 20 '21 at 14:06
  • Does this answer your question? [Using Spring Boot's ErrorController and Spring's ResponseEntityExceptionHandler correctly](https://stackoverflow.com/questions/55101797/using-spring-boots-errorcontroller-and-springs-responseentityexceptionhandler) – maio290 Apr 20 '21 at 14:08

2 Answers2

2

If you wish to execute different code based on the TYPE of the exception thrown, you should look at using @ControllerAdvice along with @ExceptionHandler. Any exceptions that you do not handle with an @ExceptionHandler will then bubble up to the default ErrorController (though you could handle Exception in a handler and then all exceptions will be handled via your custom handler). Something like:

@ControllerAdvice
@RestController
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(MyException.class)
    protected ResponseEntity<String> handleMyException(MyException ex) {
        // Your code here
    }

    @ExceptionHandler(Exception.class)
    protected ResponseEntity<String> handleException(Exception ex){
        // Your code here
    }
}

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-exceptionhandler

Ben M
  • 1,833
  • 1
  • 15
  • 24
  • I wish to get the actual exception in my `handleError` method, something like `Exception e = `, this is so i can do some other handling with it. This is because the spring default error controller prints the stacktrace in the whitelabel error page, so there has to be some way that it got the exception. @Ben M – BeABraveDude Apr 20 '21 at 14:36
  • That's the purpose of using `@ExceptionHandler`s as they provide you with the actual exception class so that you can operate on it. – Ben M Apr 20 '21 at 15:00
  • Yes, i tried to use that but it catches every single error and breaks my app. I use `Exception.class` as the parameter for it. – BeABraveDude Apr 20 '21 at 16:39
  • if you use `Exception.class` it _will_ catch every single error, by design. You should create specific `ExceptionHandler`s for each of the distinct exception types you want to take action on – Ben M Apr 20 '21 at 20:34
-1

No worries guys, I solved the problem, I just had to do some digging into springs built in error controller to get the stacktrace.

BeABraveDude
  • 45
  • 1
  • 4