简体   繁体   English

异常被多次重新抛出而未被 ExceptionHandler 捕获

[英]Exception is rethrown multiple times not being caught by ExceptionHandler

My app is a Spring Boot web application.我的应用程序是一个 Spring Boot Web 应用程序。 I'm having a unique problem catching my exception with my ExceptionHandler - I think it is because the exception is being thrown and rethrown multiple times in my app flow.我在用 ExceptionHandler 捕获异常时遇到了一个独特的问题 - 我认为这是因为在我的应用程序流程中多次抛出和重新抛出异常。

Here is a high level description of the problem:这是问题的高级描述:

  1. I throw ExceptionType1 in my service.我在我的服务中抛出 ExceptionType1。
  2. My ExceptionType1 exception is caught by my ErrorController class's error() method我的 ExceptionType1 异常被我的 ErrorController 类的 error() 方法捕获
@Override
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
    boolean isErr = (boolean) request.getAttribute("filter.error");
    if (isErr) {
        //the error was thrown by cta filter, throw ExceptionType2
        throw new ExceptionType2(errorMsg, errorCode);
    } else {
        //handle other errors

I catch the error, convert the ExceptionType1 exception to ExceptionType2 exception and rethrow it as ExceptionType2我捕捉到错误,将 ExceptionType1 异常转换为 ExceptionType2 异常并将其重新抛出为 ExceptionType2

  1. The ExceptionType2 exception thrown above is caught by my ExceptionHandler class上面抛出的 ExceptionType2 异常被我的 ExceptionHandler 类捕获
@Component  
@ControllerAdvice  
public class MyExceptionHandler {    
@ExceptionHandler({ExceptionType2.class})    
public final String handleExceptionType2(ExceptionType2 e) throws ExceptionType3 {      
throw new ExceptionType3(errorMsg, errorCode);   
}

The ExceptionHandler above has an @ExceptionHandler method that catches the ExceptionType2 error thrown in the previous step, converts this exception to ExceptionType3 and rethrows it.上面的 ExceptionHandler 有一个@ExceptionHandler 方法,可以捕获上一步抛出的 ExceptionType2 错误,将此异常转换为 ExceptionType3 并重新抛出。 5. Here's my problem. 5. 这是我的问题。 There is yet another ExceptionHandler class that is supposed to catch ExceptionType3 errors - but it is not working.还有另一个 ExceptionHandler 类应该捕获 ExceptionType3 错误 - 但它不起作用。 I put a breakpoint in my @ExceptionHandler method for ExceptionType3 and it is never hit我在我的@ExceptionHandler 方法中为 ExceptionType3 设置了一个断点,它永远不会被命中

TLDR -- I basically catch an exception in my code - then it is TLDR——我基本上在我的代码中捕捉到一个异常——然后就是

  1. converted to another exception type and转换为另一种异常类型并
  2. rethrown as the new exception type 3 times in my code.在我的代码中作为新的异常类型重新抛出 3 次。

It works the first 2 times but the last time I try to convert the exception and rethrow it as ExceptionType3 - it is not caught by my ExceptionHandler class for ExceptionType3.它在前 2 次工作,但最后一次我尝试转换异常并将其作为 ExceptionType3 重新抛出 - 它没有被我的 ExceptionHandler 类为 ExceptionType3 捕获。

My theory right now is - I have converted exception type and rethrown the exception too many times.我现在的理论是 - 我已经转换了异常类型并多次重新抛出异常。 Is there some limitation on this in Spring? Spring对此有一些限制吗? The reason I think this is - if I create and throw ExceptionType3 at any point in the above steps before step 5 - it is able to be caught by the appropriate ExceptionHandler class.我认为这是因为 - 如果我在第 5 步之前的上述步骤中的任何时候创建并抛出 ExceptionType3 - 它能够被适当的 ExceptionHandler 类捕获。

Any advice on this would be appreciated.对此的任何建议将不胜感激。

I believe that Spring does not allow handling of exceptions re-thrown from other exception handlers.我相信 Spring 不允许处理从其他异常处理程序重新抛出的异常。 Exception handler is supposed to be a final point where exception should be converted to HTTP response or just logged.异常处理程序应该是将异常转换为 HTTP 响应或仅记录的最后一点。

One of the way (maybe a little dirty) to solve your issue is to call handler for ExceptionType3 manually.解决您的问题的一种方法(可能有点脏)是手动调用ExceptionType3处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM