2 Replies Latest reply on Mar 26, 2013 6:49 AM by peter.probst

    Customize Server Error Handling

    peter.probst

      Hello,

       

      is there a way to customize the error handling on server side? We are using rpc calls and want to catch all server errors, make some computation, and then send the errors forth to the clients - but not in every rpc call, more in a generic manner. We have not found a method for doing this. Now we are deciding to use the errai code and customize the ErraiServiceImpl. But this adds an additionally maintaining burden.

      Has anyone an idea of doing this better?

      Thanks, Peter

        • 1. Re: Customize Server Error Handling
          jfuerth

          Hi Peter,

           

          While it's true that Errai doesn't provide its own server-side RPC interceptor facility, we do support CDI interceptors for RPC calls.

           

          The Weld reference guide has a chapter about interceptors: http://docs.jboss.org/weld/reference/latest/en-US/html/interceptors.html

           

          Here's the CDI interceptor code from our test suite. You could implement something similar in your app:

           

          First, make an interceptor binding annotation:

           

          @InterceptorBinding
          @Target({ ElementType.METHOD, ElementType.TYPE })
          @Retention(RetentionPolicy.RUNTIME)
          public @interface InterceptedRpc {
          }
          

           

          Then create the interceptor for that binding (note the custom @InterceptedRpc interceptor binding annotation at the class level):

           

          @InterceptedRpc
          @Interceptor
          public class RpcInterceptor {
              @AroundInvoke 
              public Object aroundInvoke(InvocationContext ctx) throws Exception {
                String parm = (String) ctx.getParameters()[0];
                parm += "_intercepted";
                ctx.setParameters(new Object[]{parm});
                return ctx.proceed();
              }
          }
          

           

          Finally, for each server-side service impl method you want your interceptor to operate on, apply your custom interceptor binding annotation:

           

          @Service
          @ApplicationScoped
          public class InterceptedRemoteImpl implements MyInterceptedRemote {
          
            @Override
            @InterceptedRpc
            public String interceptedCall(String callString) {
              return callString;
            }
          }
          

           

          Hope that helps!

           

          -Jonathan

          • 2. Re: Customize Server Error Handling
            peter.probst

            Hello Jonathan,

            thank you for your valuable information. We have tested this solution and it works like a charm. Problem solved!

            Thanks,

            -Peter