Thursday, September 16, 2010

Re: Unchecked exceptions from server to client

I managed to d that, but its quite tricky...

What we have :

- a GWT RPC controller servlet, that handles ALL RPC calls (all calls
URL are of the form "MyService.rpc"
- it uses "MyService" to resolve a Spring bean, which is used to
perform the service (we inject the request and response in the service
using a ThreadLocal bean)
- We wrote our own "invokeAndEncoderesponse" method, which does the following :

- call the service
- catch all Exceptions (InvocationTargetException, SecirutyException
and IllegalArgumentException) and wraps the cause exception in a
specific RpcServiceException which we wrote (it's a basic exception,
not Runtime) (of course, if the cause exception is already a
RpcServiceExceptionit is not wrapped)

The tricky part comes here :
Our service Interface do not extend RemoteService, but a subinterface
(RpcService), which uses a specific Generator (by setting a
generate-with tag in the ./gwt.xml file)

This generator uses a specific Proxycreator (which extends default
ProxyCreator) : it overrides 2 methods :

- addRoots, to add our RpcServiceException to the root classes of
serializable types for the service,
- getRemoteServiceRelativePath, which adds '.rpc' suffix to the
default relative path (so that developpers do not have to set it using
the GWT annotation)


This is very useful, because it garantees that exceptions handled by
Rpc service callbacks are ALWAYS RpcServiceException : we just have to
provide defaults Callbacks to manage Exception log/display on client
side.

Etienne Lacazedieu

2010/9/16 Kasper Hansen <kbhdk1976@gmail.com>:
> Hi,
>
> And thanks for your continuing interest in this subject.
>
>>    void getObject() throws RuntimeException; // or our
>> GwtClientException
>> }
>> Problem is gone... Every method from service can throw our
>> RuntimeExceptions.
>>
>> Is it feature or a bug?
>
> If you need to declare a RuntimeException, or your own exception
> inherited from RuntimeException, I would say it's a bug. Otherwise,
> what is the need for unchecked exceptions ? However, it may be tricky
> to change, since it seems glued into the way GWT works.
>
> I would be happy to be able to tell GWT, that all instances of (or
> instances of subclasses of) GWTClientRuntimeException (a classname i
> just invented) should be generated to client side code. This gives me
> the freedom to to this on the client side;
>
> try {
>   myService.callThatMightThrowUp();
> }
> catch (GWTClientRuntimeException ex) {
>  if (ex instanceof MyInheritedException1) {
>    //bla bla
>  }
>  else {
>    if (ex instanceof MyInheritedException2) {
>      //bla bla
>    }
> }
>
> I think that would be neat.
>
> What do you think ?
>
> :-) Kasper
>
>
> On Thu, Sep 16, 2010 at 1:55 PM, San0 <sanopl@gmail.com> wrote:
>> Hi again,
>>
>> I did more "research" about our way of handling exceptions.
>> Most of our service methods doesnt have any throws declarations but
>> all of them have validation, which is throwing runtime exception (here
>> we are sending field names with errors).
>>
>> "Bad" service:
>> public interface ExceptionModuleService extends RemoteService {
>>    void validate();
>>
>>    void getObject();
>> }
>>
>> In this case, code from my previous code wont work. GWT will throw
>> SerializationException, since it cant serialize GwtClientException,
>> which would be thrown in both methods.
>> BUT in this case:
>> public interface ExceptionModuleService extends RemoteService {
>>    void validate();
>>
>>    void getObject() throws RuntimeException; // or our
>> GwtClientException
>> }
>> Problem is gone... Every method from service can throw our
>> RuntimeExceptions.
>>
>> Is it feature or a bug?
>>
>> Example code: http://san0.org/gwt/gwt-ex.zip
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
>> To post to this group, send email to google-web-toolkit@googlegroups.com.
>> To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
>> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
> To post to this group, send email to google-web-toolkit@googlegroups.com.
> To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment