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);
    }

    .........
    ..........
    .........
}
aminography
  • 21,986
  • 13
  • 70
  • 74
PAA
  • 1
  • 46
  • 174
  • 282

2 Answers2

1

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);
}
Vasif
  • 668
  • 4
  • 10
  • 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! – Jadson Santos Dec 17 '21 at 12:13
0

You can configure the interceptors using WebMvcConfigurerAdapter

17.15.3 Configuring Interceptors

You can configure HandlerInterceptors or WebRequestInterceptors to be applied to all incoming requests or restricted to specific URL path patterns.

An example of registering interceptors in Java:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
     registry.addInterceptor(new GlobalExceptionHandler());

      }

  }
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98