【问题标题】:Why does the ResponseEntityExceptionHandler catch ONLY Generic Exception not specific run times为什么 ResponseEntityExceptionHandler 只捕获通用异常而不是特定的运行时间
【发布时间】:2018-02-15 21:07:41
【问题描述】:

我的自定义响应实体如下:

@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(Exception.class)
@ResponseBody
public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
logger.debug ("CustomizedResponseEntityExceptionHandler-Generic Exception caught !!  "+ex.getMessage ());
    EASExceptionNotifier exceptionResponse = new EASExceptionNotifier
    (new Date(), ex.getMessage(),request.getDescription(false));
    return new ResponseEntity(exceptionResponse,HttpStatus.INTERNAL_SERVER_ERROR);
}



@ExceptionHandler(InvalidRequestTypeException.class)
public final ResponseEntity<Object> handleInvalidRequestTypeException(InvalidRequestTypeException ex, WebRequest request) {
    EASExceptionNotifier exceptionResponse = new EASExceptionNotifier
    (new Date(), ex.getMessage(),request.getDescription(false));
    return new ResponseEntity(exceptionResponse, HttpStatus.NOT_ACCEPTABLE);
}

}

我的异常类定义如下:

public class InvalidRequestTypeException extends RuntimeException {
private static Logger logger = LoggerFactory.getLogger(InvalidRequestTypeException.class);
private static final String ERR_DESC="Error desc: Invalid request type received ";

public InvalidRequestTypeException (String storeMetaData, String message) {
    super (message);
    logger.debug ("Error Message Caught >> {} ",message);
    StringBuilder finalErrorMessage= new StringBuilder();
    finalErrorMessage.append (storeMetaData).append (message);

}

客户端代码抛出错误:

if (!(requestType.equals ("RF20"))) {
throw new InvalidRequestTypeException 
 (errorData.toString()," Received wrong request type id :" + requestType);}

问题是:CustomResponseEntity 类总是只捕获 Generic Exception 而不是 InvalidRequestTypeException?

在 CustomResponseEntity 类的两个处理程序方法中添加了一条日志语句,并注意到只调用了 Generic Exception-

com.eas.common.exception.framework.CustomizedResponseEntityExceptionHandler 
- CustomizedResponseEntityExceptionHandler-Generic Exception caught !!
 Exception occurred during execution on the exchange:

任何建议这里缺少什么?

【问题讨论】:

    标签: spring spring-boot exception-handling spring-rest


    【解决方案1】:

    因为它是我抛出 InvalidRequestType 异常的 Camel 处理器类,所以在最后它被转换为 CamelExecutionException。

    因此它总是回退到通用异常块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-22
      • 2018-05-12
      • 2023-03-21
      • 2013-03-04
      • 1970-01-01
      • 2010-09-25
      • 2012-08-27
      • 1970-01-01
      相关资源
      最近更新 更多