如何创建全局异常处理程序

时间:2020-06-19 02:23:59

标签: actframework

在actframework中,我们可以使用@catch处理异常,但是它仅在当前类和父类中起作用。如果我想创建全局异常处理程序,就像@ExceptionHandler中的SpringBoot一样,该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以在春季启动时使用@ControllerAdvice创建全局异常处理程序。下面的答案包含解释相同内容的示例代码段。

@ControllerAdvice even by setting the highest precedense for RestControllers not working as it should

答案 1 :(得分:0)

只需将@Global注释添加到您的@Catch方法中,例如

public class AppExceptionHandler {
    @Global
    @Catch(value = Throwable.class, priority = 1)
    public void logThrowable(Throwable throwable) {
        AppEntry.LOGGER.error(throwable, "Global Exception Handler: %s", throwable.getMessage());
    }
}

使用上面给定的代码,它将捕获在任何请求处理程序(例如,

)中触发的异常

enter image description here