假设我们有这样的@RestControllerAdvice
-annotated 类:
@RestControllerAdvice
public class RestResponseExceptionHandler {
@ExceptionHandler(MyBusinessException .class)
public ResponseEntity<ErrorResponse> handleMyBusinessException (MyBusinessException ex) {
return createResponseEntity(ex, ex.getErrorCode());
}
@ExceptionHandler({IllegalArgumentException.class, ValidationException.class, DataIntegrityViolationException.class})
public ResponseEntity<ErrorResponse> handleInvalidPropertyException(RuntimeException ex) {
return createResponseEntity(ex, ErrorCode.DATA_INVALID);
}
[...]
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ErrorResponse> handleRuntimeException(RuntimeException ex) {
return createResponseEntity(ex, ErrorCode.UNKNOWN);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception ex) {
return createResponseEntity(ex, ErrorCode.UNKNOWN);
}
@ExceptionHandler(WrapperException .class)
public ResponseEntity<ErrorResponse> handleWrapperException (WrapperException ex) {
Exception exception = ex.getWrappedException();
// re-dispatch exception here
}
}
对于已知的WrapperException
,是否有可能以某种方式重新调度包装的异常?
我尝试了几件事,例如重新抛出包装的 excption 或显式调用我们的 ErrorController 的自定义方法并在那里重新抛出异常,但到目前为止没有运气。