ResponseEntityExceptionHandler handles AsyncRequestNotUsableException
Closes gh-33225
This commit is contained in:
parent
5ac7e74bf2
commit
dbc69284c8
|
@ -48,6 +48,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
|
|||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
|
||||
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
|
@ -135,7 +136,8 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
|||
HttpMessageNotReadableException.class,
|
||||
HttpMessageNotWritableException.class,
|
||||
MethodValidationException.class,
|
||||
BindException.class
|
||||
BindException.class,
|
||||
AsyncRequestNotUsableException.class
|
||||
})
|
||||
@Nullable
|
||||
public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) throws Exception {
|
||||
|
@ -197,8 +199,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
|||
else if (ex instanceof HttpMessageNotWritableException theEx) {
|
||||
return handleHttpMessageNotWritable(theEx, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
|
||||
}
|
||||
else if (ex instanceof MethodValidationException subEx) {
|
||||
return handleMethodValidationException(subEx, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
|
||||
else if (ex instanceof MethodValidationException theEx) {
|
||||
return handleMethodValidationException(theEx, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
|
||||
}
|
||||
else if (ex instanceof AsyncRequestNotUsableException theEx) {
|
||||
return handleAsyncRequestNotUsableException(theEx, request);
|
||||
}
|
||||
else {
|
||||
// Unknown exception, typically a wrapper with a common MVC exception as cause
|
||||
|
@ -568,6 +573,22 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
|||
return handleExceptionInternal(ex, body, headers, status, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize the handling of {@link AsyncRequestNotUsableException}.
|
||||
* <p>By default, return {@code null} since the response is not usable.
|
||||
* @param ex the exception to handle
|
||||
* @param request the current request
|
||||
* @return a {@code ResponseEntity} for the response to use, possibly
|
||||
* {@code null} when the response is already committed
|
||||
* @since 6.2
|
||||
*/
|
||||
@Nullable
|
||||
protected ResponseEntity<Object> handleAsyncRequestNotUsableException(
|
||||
AsyncRequestNotUsableException ex, WebRequest request) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to create a {@link ProblemDetail} for any exception
|
||||
* that doesn't implement {@link ErrorResponse}, also performing a
|
||||
|
|
|
@ -57,6 +57,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
|
||||
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
|
@ -107,10 +108,6 @@ class ResponseEntityExceptionHandlerTests {
|
|||
.filter(method -> method.getName().startsWith("handle") && (method.getParameterCount() == 4))
|
||||
.filter(method -> !method.getName().equals("handleErrorResponse"))
|
||||
.map(method -> method.getParameterTypes()[0])
|
||||
.filter(exceptionType -> {
|
||||
String name = exceptionType.getSimpleName();
|
||||
return !name.equals("AsyncRequestNotUsableException");
|
||||
})
|
||||
.forEach(exceptionType -> assertThat(annotation.value())
|
||||
.as("@ExceptionHandler is missing declaration for " + exceptionType.getName())
|
||||
.contains((Class<Exception>) exceptionType));
|
||||
|
@ -316,6 +313,13 @@ class ResponseEntityExceptionHandlerTests {
|
|||
testException(new AsyncRequestTimeoutException());
|
||||
}
|
||||
|
||||
@Test
|
||||
void asyncRequestNotUsableException() throws Exception {
|
||||
AsyncRequestNotUsableException ex = new AsyncRequestNotUsableException("simulated failure");
|
||||
ResponseEntity<Object> entity = this.exceptionHandler.handleException(ex, this.request);
|
||||
assertThat(entity).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxUploadSizeExceededException() {
|
||||
testException(new MaxUploadSizeExceededException(1000));
|
||||
|
|
Loading…
Reference in New Issue