引发/包装异常的Spring ExceptionHandler

问题描述 投票:0回答:2

我找不到一个好的解决方案:在我的Spring Boot应用程序中,作为@ExceptionHandler方法,我需要为特定异常定义一个处理程序,但是对于由特定异常(即包装异常)引起的任何异常。

示例:有时我得到这个:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:541) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746) ~[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE]    
    ... 121 common frames omitted
Caused by: custom.TechRoleException: My custom TechRoleException
    at myapp.method1[..]
    at myapp.methodOuter[..]

我的自定义TechRoleException是一个异常我抛出一些Hibernate EventListener的预更新方法,直接的例外是Persistence不会发生。

但是,永远不会达到以下尝试使用我的自定义异常的方法:

@ControllerAdvice
public class GlobalExceptionHandler {

  @ExceptionHandler(TechRoleException.class)
  public String techRoleException(HttpServletRequest request, Exception ex) {
    System.out.println("Got here");
    return "home";
  }
}

这是一个类似的线程,答案是错误的,并没有回答这个问题:@ExceptionHandler for Wrapped Exception / getCause() in Spring

spring spring-boot spring-mvc exception
2个回答
1
投票

也许是这样的?

@ExceptionHandler(Exception.class)
public String techRoleException(HttpServletRequest request, Exception ex) {
if(ex instanceof TechRoleException) {
    System.out.println("Got here");
    return "home";
} else {
    throw ex; //or something else
}
}

1
投票

我最后的工作答案是处理一般异常,然后使用Apache ExceptionUtils.getRootCause()来检测我在这个通用处理程序中寻找的特定的Caused-By。

(如果它们有专用的处理程序,则其他特定的异常将不会出现在此方法中。但是如果没有专用的处理程序,则会出现异常。)这是检测某些目标Caused-By的唯一方法。

@ExceptionHandler(Exception.class)
public String handleGeneralException(HttpServletRequest request, Exception ex) {

    Throwable rootCause = ExceptionUtils.getRootCause(ex);
    if (rootCause != null && "com.myapp.TechRoleException".equals(rootCause.getClass().getName())
    {       
        //... handle this Caused-By Exception here
        // ...
    }
    // All other exceptions that don't have dedicated handlers can also be handled below...
    // ...

}
© www.soinside.com 2019 - 2024. All rights reserved.