Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StatusCodeException Vs. RuntimeException in GWT

Tags:

gwt

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);
                }
            }
like image 899
mark Avatar asked Jun 06 '11 07:06

mark


1 Answers

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;
like image 185
Gursel Koca Avatar answered Oct 17 '22 16:10

Gursel Koca