Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle wrapped exceptions in a Spring Exception Handler? [duplicate]

I have Spring MVC and jackson. When I start an incorrect request, Jackson mapping fails and UnrecognizedPropertyException is thrown. I want to handle this exception using

@ExceptionHandler
public String handle(UnrecognizedPropertyException e) {
  ...
}

However Spring wraps this exception in HttpMessageConversionException so the code above doesn't work. is it possible in Spring to handle Jackson specific (or in general library specific) exceptions?

like image 977
piotrek Avatar asked Nov 20 '22 06:11

piotrek


1 Answers

Unfortunately, UnrecognizedPropertyException is a subtype of IOException. The RequestResponseBodyMethodProcessor that handles the @RequestBody (I assume that's where the exception occurs) has special handling for IOException (interpreting as a failure of the request input stream), wrapping it in a HttpMessageNotReadableException. Additionally, the HttpMessageConverter interface is specified to throw HttpMessageNotReadableException if there is a conversion error during read.

You're going to have to deal with that no matter what (if Jackson threw unchecked exceptions instead, things might have been different).

Fortunately, since 4.3, Spring MVC's ExceptionHandlerMethodResolver (which processes @ExceptionHandler) can unwrap the cause of exceptions (see SPR-14291). As such, assuming you do not have a handler for any exceptions in the inheritance hierarchy of HttpMessageNotReadableException, your handler method

@ExceptionHandler
public String handle(UnrecognizedPropertyException e) {
    ...
}

will be used to handle the exception. This happens after Spring MVC looks for a handler method that could handle a HttpMessageNotReadableException, then unwraps the nested exception with Throwable#getCause and tries the lookup again.


In pre-4.3, or if you do have a handler for an exception type in HttpMessageNotReadableException's inheritance hierarchy, you can always delegate after extracting the cause yourself.

@ExceptionHandler
public String handle(HttpMessageConversionException e) throws Throwable {
    Throwable cause = e.getCause();
    if (cause instanceof UnrecognizedPropertyException) {
        handle((UnrecognizedPropertyException) cause);
    }
    ...
}

public String handle(UnrecognizedPropertyException e) {
    ...
}
like image 133
Sotirios Delimanolis Avatar answered Dec 19 '22 19:12

Sotirios Delimanolis