P
P
Pavel Talaiko2020-02-05 15:07:39
Java
Pavel Talaiko, 2020-02-05 15:07:39

Why does the request hang when exceptions are thrown?

A very interesting situation is obtained.
When I use ExceptionHandler the request hangs waiting for a response.

ExceptionHandler:

@RestControllerAdvice
public class ExceptionAPIHandler {
    
    @ExceptionHandler(value = SourceException.class)
    public ResponseEntity<DataException> handleException(SourceException exception) {
        final HttpStatus code = exception.getCode();
        final DataException dataException = new DataException(exception.getMessage(), code);
        return ResponseEntity.status(code).body(dataException);
    }
}

And the most interesting thing is here. Working code:

ExceptionHandler:
@RestControllerAdvice
public class ExceptionAPIHandler {

    @ResponseBody
    @ExceptionHandler(value = SourceException.class)
    public ResponseEntity<?> handleException(SourceException exception) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new DataException(exception.getMessage(), exception.getCode()));
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question