StatusCodeException与Vs. GWT中的RuntimeException

mar*_*ark 7 gwt

在我的GWT应用程序中.我重写了RemoteServiceServlet,以便在调用服务方法之前检查会话是否有效.我试图从服务器抛出一个RuntimeException("过期的会话"),我希望客户端从asynccallback onFailure中捕获此异常...

在客户端我想:Asynccallback:

@Override
    public void onFailure(Throwable caught) {
        final String message = caught.getMessage();

        if (!isNullOrEmptyString(message) && message.contains("expired session")) {
            com.google.gwt.user.client.Window.Location.reload();
        }

    }
Run Code Online (Sandbox Code Playgroud)

但是,在客户端中,捕获的对象仍然是StatusCodeException,并且消息仍然是默认的"...服务器中的异常...".如果它是我从服务器发送的会话过期消息,我如何覆盖至少要比较的默认消息?

谢谢

嗨Gursel,这是我的代码: - > Custom RemoteServiceServlet.我试图在调用之前"拦截"每个方法.我检查会话并抛出一个RuntimeException,如果它已经过期了.所以基本上,抛出异常的声明方法不是自定义RemoteServiceServlet.它仍然转到客户端异步中的"onFailure",但Throwable对象仍然是"StatusCodeException"类型,没有EXPIRED_SESSION_MSG消息.不要知道如何做这项工作.谢谢!

public class XRemoteServiceServlet extends RemoteServiceServlet {
    private final static String EXPIRED_SESSION_MSG = "ERROR: Application has expired session.";   
    @Override
    protected void onAfterRequestDeserialized(RPCRequest rpcRequest) {
        HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
        HttpSession session = httpServletRequest.getSession(false);
        if (session != null) {
            final String sessionIdFromRequestHeader = getSessionIdFromHeader();
            if (!isNullOrEmptyString(sessionIdFromRequestHeader)) {
                final String sessionId = session.getId();

                if (!sessionId.equals(sessionIdFromRequestHeader)) {
                    throw new RuntimeException(EXPIRED_SESSION_MSG);
                }
            }
Run Code Online (Sandbox Code Playgroud)

Gur*_*oca 5

RuntimeExceptions由gwt应用程序的服务器端抛出的所有内容都已被包装,就StatusCodeException好像您没有在远程方法声明中声明它们一样.

编辑:

之后,Thomas Broyer评论说,我已经了解到在远程方法声明中声明的所有异常(已检查或未检查)都会传播到gwt客户端.因此,您所要做的就是声明您的远程方法,例如:

public void myRemoteMethod() throws RuntimeException;
Run Code Online (Sandbox Code Playgroud)

  • 不确定那个但是......如果你在接口上有明确的`throws`声明,你不能将RuntimeExceptions传播到客户端吗? (2认同)