Handle request factory server exception in client

97 views
Skip to first unread message

Yan

unread,
Dec 17, 2013, 4:20:08 PM12/17/13
to google-we...@googlegroups.com
Hi there,

I am using GWT 2.5.1 request factory. On client, after I fire the request, I have this block to display any error message from server.           

            public void onFailure(ServerFailure error)  {
                // do something with error.getMessage()
            }


On server, I extended ServceLayerDecorator like this,  but I do not know how to handle exception so that the message will show up in ServerFailure.getMessage().

I tried simply rethrowing the exception, I am getting HTML text such as server 500 error in ServerFailure.getMessage().

    @Override
    public Object invoke(Method domainMethod, Object... args) {        
        try {
            // some business logic such as check user role, etc. 
            return super.invoke(domainMethod, args);
        } catch (Exception ex) {
            throw new RuntimeException(ex); 
        }       
    }

How does this exception on Server side gets translated into ServerFailure on client and who is doing that? Maybe that is where I need to override?

Thanks,
Yan

Jens

unread,
Dec 17, 2013, 5:03:10 PM12/17/13
to google-we...@googlegroups.com
You can call report() or die() depending on what you want to do. report() re-throws a ReportableException while die() will re-throw an UnexpectedException.

Also with your above implementation you are "hiding" the exception logic of super.invoke() as it might also call report() or die() when it encounters certain exceptions. I think a better implementation would be

    @Override
    public Object invoke(Method domainMethod, Object... args) {        
        try {
            // some business logic such as check user role, etc. 
        } catch (Exception ex) {
            // only catch exceptions from your custom code
            // call report() or die()
        }   
        return super.invoke(domainMethod, args); // assume super.invoke() will do the right thing if it encounters exceptions
    }


The generation of a ServerFailure is done in the DefaultExceptionHandler. But you can also provide your own implementation by extending the RequestFactoryServlet and provide your custom exception handler through the constructor.

-- J.
Reply all
Reply to author
Forward
0 new messages