Catching & Handling Jackson Exceptions with a custom message

Found this question with a similar issue, only mine was a different JSON parse error:

JSON parse error: Unrecognized character escape 'w' (code 119); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unrecognized character escape 'w' (code 119)\n at [Source: (PushbackInputStream); line: 1, column: 10] 

coming from a REST JSON request like so

{"query":"\\w"}

If you can modify the Rest Controller, you can catch the JSON parse error with an HttpMessageNotReadableException (worked for me in Spring Boot using a @RestController annotation). Even though I could not catch the error with @ExceptionHandler(Exception.class)

You can respond with custom JSON by using a serialized object (naturally converts to JSON). You can also specify that you want the request and exception which caused the issue in the first place. So you can get details, and or modify the error message.

@ResponseBody
@ExceptionHandler(HttpMessageNotReadableException.class)
private SerializableResponseObject badJsonRequestHandler(HttpServletRequest req, Exception ex) {

    SerializableResponseObject response = new SerializableResponseObject(404,
                "Bad Request",
                "Invalid request parameters, could not create query",
                req.getRequestURL().toString())

    Logger logger = LoggerFactory.getLogger(UserController.class);
    logger.error("Exception: {}\t{}\t", response);

    return response;
}

The code would return something like

{
  "timestamp": "Thu Oct 17 10:19:48 PDT 2019",
  "status": 404,
  "error": "Bad Request",
  "message": "Invalid request parameters, could not create query",
  "path": "http://localhost:8080/user/query"
}

And would log something like

Exception: [Thu Oct 17 10:19:48 PDT 2019][404][http://localhost:8080/user/query][Bad Request]: Invalid request parameters, could not create query

Code for the SerializableResponseObject

public class SerializableResponseObject implements Serializable {
    public String timestamp;
    public Integer status;
    public String error;
    public String message;
    public String path;

    public SerializableResponseObject(Integer status, String error, String message, String path) {
        this.timestamp = (new Date()).toString();
        this.status = status;
        this.error = error;
        this.message = message;
        this.path = path;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public Integer getStatus() {
        return status;
    }

    public String getError() {
        return error;
    }

    public String getMessage() {
        return message;
    }

    public String getPath() {
        return path;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public void setError(String error) {
        this.error = error;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String toString() {
        return "[" + this.timestamp + "][" + this.status + "][" + this.path + "][" + this.error + "]: " + this.message;
    }
}

Try something along the lines of:

@ControllerAdvice
public class ExceptionConfiguration extends ResponseEntityExceptionHandler {

    @ExceptionHandler(JsonMappingException.class) // Or whatever exception type you want to handle
    public ResponseEntity<SomeErrorResponsePojo> handleConverterErrors(JsonMappingException exception) { // Or whatever exception type you want to handle
        return ResponseEntity.status(...).body(...your response pojo...).build();
    }

}

Which allows you to handle any type of exception and respond accordingly. If the response status is always the same just stick a @ResponseStatus(HttpStatus.some_status) on the method and call ResponseEntity.body(...)