Search code examples
gwtrequestfactory

GWT requestfactory: How to catch the exception i thrown in Locator at server side?


At client side:

factory.find(proxyId).fire(new Receiver<P>()
{
    @Override
    public void onSuccess( P response )
    {
        proxy = response;
        ...
    }

    @Override
    public void onFailure( com.google.web.bindery.requestfactory.shared.ServerFailure error )
    {
        Window.alert( error.getMessage() );
    }
}

at server side i use an Locator like below:

public class myLocator extends Locator<T, String>
{
    @Injector LocatorHook hook;

    @Override
    public T find( Class<? extends T> clazz, String id )
    {
        T result = ...;
        hook.run( result );
        return result;
    }

    ....
}

The hook.run() method may throwRunTimeException("validation exception") there, i expect to catch the exception at client side in onFailure(), however, i did catch the exception, but the message is "Internal Server Error", not the exception i thrown in hook.run():"validation exception".

Any ideas to let client catch the exception i throw at server side?


Updation: As Thomas said it's weird that validating objects that come fresh from data store, but i encounter a situation that i don't know how to use service method: At client i get EntityProxyId object, through the factory.find( proxyId ).fire(...) i can get the entity from datastore, but the entity may not suitable for the user to access, in this situation i need to check it at server side, but i can't find a suitable place to do the validation, Any ideas about this?


Solution

  • RequestFactory doesn't expect exceptions to be thrown by locators. Exceptions should only be thrown by service methods, and will be directed to the appropriate Receiver on the client-side (the one attached to the service method that threw).

    Outside service methods, the only exceptions that gets routed to the client are ReportableExceptions, that can only be thrown from a ServiceLocatorDecorator's report() methods. That means you could hook your own ServiceLocatorDecorator that catches exceptions from your locators and report()s them.


    That said, validating objects that come fresh from your data store seems weird. You might want to provide a ServiceLocatorDecorator that overrides validate() (that'll validate the objects after the changes coming from the client have been applied). The errors will go back to the client in the Receiver's onConstraintViolations, and the RequestContext will be unfrozen so you can further edit your proxies and fire() again.