本文介绍了StatusCodeException VS. GWT中的RuntimeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GWT应用程序中。在调用服务方法之前,我将覆盖RemoteServiceServlet以检查会话是否有效。我试图从服务器抛出一个RuntimeException(expired session),我希望客户端从asynccallback on on the following失败中捕获这个异常...



在客户端我想:
Asynccallback:

  @Override 
public void onFailure(Throwable caught){
final String message = caught.getMessage(); $!
$ b if(!isNullOrEmptyString(message)&& message.contains(expired session)){
com.google.gwt.user.client.Window.Location.reload( );
}

}

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



谢谢



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

  public class XRemoteServiceServlet扩展RemoteServiceServlet {
private final String EXPIRED_SESSION_MSG =错误:应用程序已经过期会话。 ;
@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);
}
}


解决方案

全部如果你没有在你的远程方法声明中声明它们,gwt应用程序的服务器端抛出的 RuntimeExceptions 已被封装为 StatusCodeException

编辑:

之后,Thomas Broyer评论道,在远程方法声明中声明的所有异常(选中或未选中)都会传播到gwt客户端。因此,你所要做的只是声明你的远程方法,比如:

  public void myRemoteMethod()throws RuntimeException; 


In my GWT app. I overrode RemoteServiceServlet to check if the session is valid right before the service method is being called. I am trying to throw a RuntimeException("expired session") from the server and I would like the client to catch this exception from the asynccallback onFailure...

In the client I would like to: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();
        }

    }

However, in the client, the caught object is still a StatusCodeException and the message is still the default "...Exception in the server...". how can I override the exception at least the default message to compare if it was a session expired message I sent from the server?

thanks

Hi Gursel,Here's my code:-> Custom RemoteServiceServlet. I'm trying to "intercept" every method before it's invoked. I check the session and throw a RuntimeException if it's already expired. So basically, it is not the declared method that throws the exception but the custom RemoteServiceServlet. It still goes to the "onFailure" in the client async but the Throwable object is still of type "StatusCodeException" without the EXPIRED_SESSION_MSG message. Don;t know how to make this work. Thanks!

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);
                }
            }
解决方案

All RuntimeExceptions thrown by Server side of gwt application has been wrapped as StatusCodeException if you did not declare them at your remote method declaration.

EDIT :

After, Thomas Broyer comment, I have learned that all exceptions (checked or unchecked) that are declared at remote method declaration are propagated to gwt client. Therefore all you have to do is just declare your remote method such as :

public void myRemoteMethod() throws RuntimeException;

这篇关于StatusCodeException VS. GWT中的RuntimeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:39