question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle specific reasons of HttpMessageNotReadableException

See original GitHub issue

Something like this

    @ExceptionHandler
    public ResponseEntity<Problem> onHttpMessageNotReadable(final HttpMessageNotReadableException e) throws Throwable {
        final Throwable cause = e.getCause();
        if (cause == null) {
            return onException(e);
        } else if (cause instanceof JsonParseException) {
            return onParseException((JsonParseException) cause);
        } else if (cause instanceof JsonMappingException) {
            return onMappingException((JsonMappingException) cause);
        } else {
             ...
        }
    }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
timothy-mugayicommented, Sep 26, 2018

I was hoping there was a more elegant way of hanlding exceptions instead of having to do below

@ExceptionHandler
    public ResponseEntity<Problem> onHttpMessageNotReadable(final HttpMessageNotReadableException e) throws Throwable {
        final Throwable cause = e.getCause();
        if (cause == null) {
            return onException(e);
        } else if (cause instanceof JsonParseException) {
            return onParseException((JsonParseException) cause);
        } else if (cause instanceof JsonMappingException) {
            return onMappingException((JsonMappingException) cause);
        } else {
             ...
        }
    }
1reaction
lukasniemeier-zalandocommented, Sep 9, 2015

Alternatively you can throw and catch it. So you don’t need the instanceof checks.

If you throw an exception from a ExceptionHandler, will it be catched by any ControllerAdvice again? I think I’ve seen JsonParseException (not wrapped inside a HttpMessageNotReadableException), which would be cool for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - @ExceptionHandler doesn't catch ... - Stack Overflow
You have to specify the class that the handler needs to handle. Like: @ExceptionHandler(HttpMessageNotReadableException.class).
Read more >
Guide to Spring Boot REST API Error Handling - Toptal
I have a question on how to log the error in RestExceptionHandler to identify which API call cause the error. I think the...
Read more >
Error Handling for REST with Spring
1. Restful API Error / Exception Design · The status represents HTTP status code. · error_code represents REST API specific error code. ·...
Read more >
Additional Handling of Jackson Parsing and Mapping Errors
Common handling of JSON parsing/mapping exceptions. ... @ExceptionHandler(HttpMessageNotReadableException.class) public ...
Read more >
HttpMessageNotReadableExcep...
BAD_REQUEST) public R handleError(HttpMessageNotReadableException e) { log.error("消息不能读取:{}", e. ... ControllerExceptionMapper.handle(...).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found