7

I am using spring boot and write a global exception handler use AbstractErrorController. How could i get an exception object in controller?

@Controller
public class MyCustomErrorController extends AbstractErrorController {

    public MyCustomErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @RequestMapping("/error")
    public void handleError(HttpServletRequest req, HttpServletResponse resp) {
        Exception e = ...; // how to get exception here
        log.error(e);
        displayError(req, resp, e);
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
1
  • I don't understand the logic here. Do you want to retrieve an error from an endpoint? Usually error handlers catch an exception and return an error for any controller that thrown that exception. Commented Jan 21, 2019 at 15:32

4 Answers 4

5

You can get the exception from the HttpServletRequest as follows:

    @Controller
    public class MyCustomErrorController extends AbstractErrorController {
    
        @RequestMapping("/error")
        public void handleError(HttpServletRequest request) {
            Exception e = (Exception) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
            ...
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

If this ErrorController pick up other error like resource not found from your database, or invalid endpoint /dsaklfja, the exception casted here is null
1

this is how it works in Spring Boot 3.1.0. But should work in previous versions as well:

import org.springframework.web.servlet.DispatcherServlet;

   @Controller
    public class MyCustomErrorController extends AbstractErrorController {
    
        @RequestMapping("/error")
        public void handleError(HttpServletRequest request) {
                   Exception e = (Exception) request.getAttribute(DispatcherServlet.EXCEPTION_ATTRIBUTE);

            ...
        }
    }

Comments

0

An handler intercepts an Exception generated or re-thrown by a controller. It doesn't have an endpoint because it usually does it for all the controllers in your application. The Handler instructs the application server to return a specific error when a specific Exception is thrown.

Here is an example:

@ControllerAdvice // Specialization of @Component for classes that declare @ExceptionHandler, @InitBinder, or @ModelAttribute methods to be shared across multiple @Controller classes.
public class ResourceNotFoundExceptionHandler {

@ExceptionHandler(value = { ResourceNotFoundException.class })
public ResponseEntity<Object> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
    ApiError error = new ApiError(HttpStatus.NOT_FOUND,  ex.getLocalizedMessage(), ex);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

    ResponseEntity<Object> response = new ResponseEntity<>(error, headers, HttpStatus.NOT_FOUND);

    return response;
}
}

In this example ApiError is a data structure that reports the error to the UI. What this code does is intercepting the Exception "ResourceNotFoundException", create an appropriate error Data transfer object, set the response HttpStatus and headers and return the error.

you can find a different example here: https://github.com/otrebor/springbootseed-openshift/blob/master/src/main/java/com/company/example/springbootseed/core/errorhandling/handlers/

4 Comments

But ControllerAdvice dosen't catch exception from filter.
You can get the endpoint point of which ever url failed by : String url = (String) request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
0

Add Exception as an extra parameter to handleError()

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.