3 Replies Latest reply on Aug 26, 2015 2:05 PM by bsgomes

    Errai JAX-RS throwing and catching exceptions (howto?)

    yagerhard

      I'm having some difficulties working out how to throw a userException from a JAX-WS serverImpl class and then catch it nicely in the client class.

       

      The service definition is like this:

      ---------------------------------------------

      @Path("blah")

      public interface BlahService {

       

          @POST

          @Consumes("application/json")

          @Produces("application/json")

          public MyResult fooOperation(MyRequest myRequest) throws myException;

      ---------------------------------------------

       

      The ServiceImpl class would look like this:

       

      ---------------------------------------------

      public classBlahServiceImpl implements BlahService{

       

          @Override

          public MyResult fooOperation(MyRequest myRequest) throws myException{

              If (happy days){

                  // Do something

                  return new MyResult();

              } else {

                throw new MyException("myException");

              }

          }

      ---------------------------------------------

       

      The client would look like this

      ---------------------------------------------

       

        private void letsDoIt(){

              MyRequest myRequest = new MyRequest();

              try {

                  blahService.call(blahCallback).fooOperation(myRequest);

                  System.out.println("Success");

              } catch (MyException e) {

                  System.out.println("Error [" + e.getMessage() + "]");

              }

          }

       

        final RemoteCallback<MyResult> blahCallback = new RemoteCallback<MyResult>() {

              @Override

              public void callback(MyResult myResult) {

                  // Do something here with myResult

                  System.out.println("Doing something useful with the result");

              }

          };

       

      -------------------------------------------

      When I call "letsDoIt" from the client, The sever gets invoked correctly and the result is returned and everything is great.

      If however, I throw an exception in the Impl class, the "catch (MyException e)" code in "letsDoIt" does not get called. As far as "letsDoIt" is concerned, the operation was a success (the console even prints out "success". However, when the method completes, the underlying Erria error handling notices that an exception has occurred and proceeds to produce stack traces etc. I've tried including an errorHandler callback like this: "blahService.call(blahCallback, myErrorCallback).fooOperation(myRequest);", but this seems to aimed at more cataclysmic communication errors rather than simple application exceptions.

       

      So how should I be catching "MyException" in the client?

       

      Many thanks,

       

      MarkB

        • 1. Re: Errai JAX-RS throwing and catching exceptions (howto?)
          csa

          Hi Mark,

           

          JAX-RS supports ExceptionMappers that allow you to map your custom exceptions to specific HTTP responses e.g. you could map MyException to an HTTP response with status code 405 (all details can be found here: http://docs.jboss.org/resteasy/docs/2.3.6.Final/userguide/html_single/index.html#ExceptionHandling)

           

          On the client, however, you will have to map the HTTP response code back to your custom exception type. In addition to the remoteCallback you can provide an errorCallback instance which will be invoked in case an exception is thrown on the server. Within the error method you can check the status code and then throw the corresponding exception type.

           

          You can find examples for response/error handling for Errai 3 here: https://docs.jboss.org/author/display/ERRAI/Errai+JAX-RS#ErraiJAX-RS-HandlingResponses

          For Errai 2: http://docs.jboss.org/errai/2.3.0.Final/errai/reference/html_single/#sid-19398997_ErraiJAX-RS-HandlingResponses

           

          While this is easy, and can probably be factored out into an app specific utility, mapping the status code back to the exception type involves some boilerplate code. I am thinking we could add support for ExceptionMappers on the client. Then the proxies could directly pass the corresponding exception to the error callback. So, the second step would fall away. I might be able to get to this next week if you think it's useful. Feel free to create a JIRA for us.

           

          In any case, the reason you can't just catch the exception directly on the client after calling the resouce method (e.g. fooOperation in your example above) is that the request is asynchronous. That's why you need to provide a callback.

           

          Cheers,

          Christian

          • 2. Re: Errai JAX-RS throwing and catching exceptions (howto?)
            yagerhard

            Hi Christian,

             

            Thanks for the prompt reply. Anything you can do to remove boiler-plate code is most welcome. It's definately better that one of you guys sort this out once (really well) and save the rest of the app developers from endlessly re-implementing (potentially poor) solutions.

            A really nice result would be if the ErrorCallback just served up "MyException" :-)

             

            As MyException is @Portable, if I included some other shared @Portable objects in the exception, whould you envisage these also being reconstructed so the app developer doesn't have to think about it?

             

            I'll certainly raise the jira as you suggested - I think to would be a significant plus for Errai JAX-WS.

             

            Cheers,

             

            MarkB

            • 3. Re: Errai JAX-RS throwing and catching exceptions (howto?)
              bsgomes

              Hi, Christian!

               

              I know this is an old post, but i'm trying to use the ClientExceptionMapper on Errai annotated with @Provider but its not working.

               

              On JaxrsProxyLoaderGenerator line 128, when it tries to get all providers with the annotation, the method always returns null.

               

              Do i need another configuration to make it work or only the annotation?

               

              I'm using 3.0.4.final.

               

              Regards.

               

              Bruno