DEV Community

Clayton Walker
Clayton Walker

Posted on

Snippet: How to handle non-issue (client) exceptions in spring boot

If you find yourself writing your own exception handlers, you might have also found yourself manually filtering out 'known-good' exceptions, such as exceptions caused by clients disconnection.

No sense in replicating spring's existing filter, just use DisconnectedClientHelper.isClientDisconnectedException) in your @ControllerAdvice or @ExceptionHandler.

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public ResponseEntity<?> handle(Exception e, WebRequest request) {
        if (DisconnectedClientHelper.isClientDisconnectedException(e)) {
            // handle as usual
            return null;
        }
        // handle other exceptions
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)