1

I am working on Spring Boot Example and implemented GlobalExceptionHandler and trying to print all error messages in JSON - it's my custom method.

Also, I have ExceptionHandler there I am catching all the Exception. But is there any way to pass the exception object from ResponseEntityExceptionHandler to HandlerInterceptor?

HandlerInterceptor:

@Slf4j
public class GlobalExceptionHandler implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        ............
        .............
        ..............
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {

        ServletRequestAttributes attributes = (ServletRequestAttributes) request.getAttribute(REQUEST_ATTRIBUTES);
        ServletRequestAttributes threadAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();    
        ............
        .............
        ..............
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        if(ex != null) {
            printJsonReq(request, response);
        }
    }
}

ExceptionHandler:

@ControllerAdvice
@Slf4j
public class ExceptionHandler extends ResponseEntityExceptionHandler{

    @ExceptionHandler({ResponseStatusException.class})
    protected ResponseEntity<Object> handleResStatusException(Exception e, WebRequest request, HttpServletRequest httpRequest) {
        ResponseStatusException be = (ResponseStatusException) e;

        ErrorResource error = ErrorResource.builder().code(AppConst.BAD_REQUEST)
                .message(ExceptionUtils.getDetails(e.getCause())).build();

        return handleExceptionInternal(e, error, getHeaders(), HttpStatus.BAD_REQUEST, request);
    }

    .........
    ..........
    .........
}

1 Answer 1

2

You can set it as a request attribute in ExceptionHandler class (if you need it just to be sure you are going print log then instead of passing Exception object you can pass boolean param to not load your request object)

request.setAttribute("exception", e);

And use it in your HandlerInterceptor as

if(ex != null || request.getAttribute("exception") != null) {
   printJsonReq(request, response);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing! I have been looking for this for 2 days. I wanted to log the exception into an Interceptor, but when using a ExceptionHandler, the ExceptionHandler would silence the exception. It is smart solution, we have access to the request object on both sides, so it's possible to send data by request. That solved my problem! Thanks!

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.