|
48 | 48 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
49 | 49 | import org.springframework.web.context.request.ServletWebRequest;
|
50 | 50 | import org.springframework.web.context.request.WebRequest;
|
| 51 | +import org.springframework.web.context.request.async.AsyncRequestNotUsableException; |
51 | 52 | import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
|
52 | 53 | import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
53 | 54 | import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
@@ -135,7 +136,8 @@ protected MessageSource getMessageSource() {
|
135 | 136 | HttpMessageNotReadableException.class,
|
136 | 137 | HttpMessageNotWritableException.class,
|
137 | 138 | MethodValidationException.class,
|
138 |
| - BindException.class |
| 139 | + BindException.class, |
| 140 | + AsyncRequestNotUsableException.class |
139 | 141 | })
|
140 | 142 | @Nullable
|
141 | 143 | public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) throws Exception {
|
@@ -197,8 +199,11 @@ else if (ex instanceof HttpMessageNotReadableException theEx) {
|
197 | 199 | else if (ex instanceof HttpMessageNotWritableException theEx) {
|
198 | 200 | return handleHttpMessageNotWritable(theEx, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
|
199 | 201 | }
|
200 |
| - else if (ex instanceof MethodValidationException subEx) { |
201 |
| - return handleMethodValidationException(subEx, headers, HttpStatus.INTERNAL_SERVER_ERROR, request); |
| 202 | + else if (ex instanceof MethodValidationException theEx) { |
| 203 | + return handleMethodValidationException(theEx, headers, HttpStatus.INTERNAL_SERVER_ERROR, request); |
| 204 | + } |
| 205 | + else if (ex instanceof AsyncRequestNotUsableException theEx) { |
| 206 | + return handleAsyncRequestNotUsableException(theEx, request); |
202 | 207 | }
|
203 | 208 | else {
|
204 | 209 | // Unknown exception, typically a wrapper with a common MVC exception as cause
|
@@ -568,6 +573,22 @@ protected ResponseEntity<Object> handleMethodValidationException(
|
568 | 573 | return handleExceptionInternal(ex, body, headers, status, request);
|
569 | 574 | }
|
570 | 575 |
|
| 576 | + /** |
| 577 | + * Customize the handling of {@link AsyncRequestNotUsableException}. |
| 578 | + * <p>By default, return {@code null} since the response is not usable. |
| 579 | + * @param ex the exception to handle |
| 580 | + * @param request the current request |
| 581 | + * @return a {@code ResponseEntity} for the response to use, possibly |
| 582 | + * {@code null} when the response is already committed |
| 583 | + * @since 6.2 |
| 584 | + */ |
| 585 | + @Nullable |
| 586 | + protected ResponseEntity<Object> handleAsyncRequestNotUsableException( |
| 587 | + AsyncRequestNotUsableException ex, WebRequest request) { |
| 588 | + |
| 589 | + return null; |
| 590 | + } |
| 591 | + |
571 | 592 | /**
|
572 | 593 | * Convenience method to create a {@link ProblemDetail} for any exception
|
573 | 594 | * that doesn't implement {@link ErrorResponse}, also performing a
|
|
0 commit comments