4 Replies Latest reply on Feb 8, 2011 5:32 AM by TomBurgundy

    Exception deserializing from BlazeDS with the help of [RemoteClass] metadata

    Avangel

      Hi,

       

      In my BlazeDS application (AMF based), I am throwing two kind of java Exceptions:

      • TechnicalException extends RuntimeException : this one is thrown to the Flex client when any unexpected error on the server is caught
      • BusinessException extends Exception : this one is a base class for business-related exception

       

      On the Flex side, I created the same Exception classes mapped to the Java ones using the [RemoteClass] metadata tag.

       

      In the error event handler of my RemoteObject call, I am looking at the event.fault.rootCause property for fully typed flex exception class, so that I can simply switch my behavior depending on the received exception. I observed the following behavior:

      • When throwing a java TechnicalException from the server, it is correctly deserialized as a flex TechnicalException on client side. So the assertion "event.fault.rootCause is TechnicalException" is working fine.
      • When throwing a java BusinessException from the server, it is NOT correctly deserialized as a flex BusinessException on client side. So the assertion "event.fault.rootCause is BusinessException" is NOT working.

       

      The only reason I see for that is that only RuntimeExceptions can be deserialized strongly typed on Flex side. Is it the nominal behavior of the Flex RemoteObject or did I miss something ?

       

      Many thanks in advance for your help,

      Avangel.

        • 1. Re: Exception deserializing from BlazeDS with the help of [RemoteClass] metadata
          Avangel Level 1

          Update: it seems that only Java exceptions that directly inherit from RuntimeException get correctly deserialized on Flex side. I suppose this is the expected behavior ? Anybody can confirm ?

          • 2. Re: Exception deserializing from BlazeDS with the help of [RemoteClass] metadata
            Avangel Level 1

            Ok, I finally found the answer to my problem. For those interested, the cast of java Exceptions into ActionScript Exception is working exactly like any other class. It has nothing to do with whether the java class extends RuntimeException or not, or any other class / interface. It just doesn't matter at all. Flex RPC simply looks for an AS class mapped to the received Java class through the [RemoteClass] metadata tag. If it finds it, it just try to map fields from java object to AS object (even if a field is not matching, it just ignores it).

             

            So the problem was a very well-know AS compiler problem: AS classes that are used only at runtime for conversion are not compiled in the SWF. In other words, if you have not any class that references your Exception class, that one won't be included in the SWF file, so the RemoteObject won't find it, thus returns an Object instead of strongly typed AS class.

             

            Hope this may help someone

             

            Avangel.

            • 3. Re: Exception deserializing from BlazeDS with the help of [RemoteClass] metadata
              msakrejda Level 4

              Glad you were able to figure it out and thanks for sharing the solution.

              So the problem was a very well-know AS compiler problem: AS classes that are used only at runtime for conversion are not compiled in the SWF.

              Strictly speaking, I think this is a method of optimizing SWF size rather than a compiler problem. However, this is a prime example of premature optimization being the root of all evil. I think this has caused me more headaches than all other ActionScript problems combined.

              • 4. Re: Exception deserializing from BlazeDS with the help of [RemoteClass] metadata
                TomBurgundy Level 1

                Yes, and also it only works for concrete subclasses, not supertypes!

                 

                For example, I just tried to get all Spring Security AuthenticationException classes to map to a class of the same name in Flex, so that I could check on that type. Doesn't work.

                 

                Now that I have a class dedicated to the actual CredentialsExpiredException it works.