简体   繁体   English

在EJB3中处理拦截器中的异常的正确方法是什么?

[英]What is the proper way to deal with Exceptions in Interceptors in EJB3?

I want to use an interceptor for my bean, which will check the validity of a given connection token. 我想对我的bean使用拦截器,它将检查给定连接令牌的有效性。

If the connection is invalid, I want to throw a particular exception, if the connection expired, I want to send another (TokenExpiredException, something like this). 如果连接无效,我想抛出一个特定的异常,如果连接过期,我想发送另一个(TokenExpiredException,类似这样的东西)。 These Exceptions are included in the interface given to the client. 这些异常包含在提供给客户端的接口中。

@AroundInvoke
public Object checkParams(InvocationContext ctx) throws TokenExpiredException, Exception{
    //code to check token
    //...
    throw new TokenExpiredException(); 
}

From what I tried, throwing such specific Exception in the Interceptor leads to an UndeclaredThrowableException on the client side. 根据我的尝试,在Interceptor中抛出这种特定的Exception会导致客户端的UndeclaredThrowableException While this exception includes the reference to the cause, it is not really ideal, and can't be dealt with with regular catch clauses. 尽管此异常包括对原因的引用,但它并不是真正理想的方法,并且不能通过常规catch子句处理。

What is the correct way then to declare different Exception types with Interceptors? 那么使用拦截器声明不同的异常类型的正确方法是什么?

I don't think there is a correct way to do that. 我认为没有正确的方法可以做到这一点。 Methods should throw only the exceptions they declared, and an interceptor shouldn't add a new one. 方法应该只抛出它们声明的异常,并且拦截器不应该添加新的异常。

My personal case got fixed by adding an error code to our default exception which is thrown by all methods. 通过将错误代码添加到所有方法抛出的默认异常中,我的情况得到了解决。

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

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