Flex Pasta » Exception Handling with BlazeDS and Flex

Exception Handling with BlazeDS and Flex

A common hurdle a developer may face is dealing with exceptions in BlazeDS. When an exception is thrown in Java, how do we handle this in flex? Here is a simple and flexible approach inspired by Scott Morgan.

1. Create a Java Class that extends RuntimeException.

package com.flexpasta.exception;
public class FlexException extends RuntimeException
{
public FlexException(String message)
{
super(message);
}
}

2. Create a matching flex class called FlexException, and put the usual RemoteObject tag in their so BlazeDS will know what to look for. Also remember in actionscript, each object must be instantiated at least once, so be sure to do that somewhere with FlexException.

3. In Java, whenever there is a known exception, or some error where the end user needs to be shown a message, throw a FlexException and pass in the message the user should see in the constructor. Let’s say the user just entered an invalid password. throw new FlexException(”Wrong Password Entered, Please Try Again.”);

4. Flex receives the Exception in the form of a fault. In the fault handler, you call this method:

/**
* Parse the incoming fault for an FlexException class. If the exception class is found and it has a matching actionscript class,
* show an alert to the user with the correspoding messages. If we don't recognize the Exception type, show a generic error.
*
* @param fault
*
*/
public static function handleException(fault:Fault): void
{
var msg : String = fault.faultString;
var clazz : Class = getExceptionClass(fault);
var instance : Error = null;
if (clazz != null)
{
Alert.show(msg,'Error');
}
else
{
Alert.show('Error! Please try again. If this issue persists, contact the system administrator');
}
}
/**
*
* Return the Class corresponding to the exception by parsing the fault string. If a corresponding actionscript error class is found,
* return it. Otherwise, return null.
*
* @return
*
*/
public static function getExceptionClass(fault:Fault) : Class
{
var clazz:Class = null;
var index:int = myFault.faultString.indexOf("FlexException");
if (index != -1)
{
var cname:String = myFault.faultString.substr(0,index-1);
try
{
clazz = getDefinitionByName("com.flexpasta.FlexException") as Class;
}
catch (e:ReferenceError)
{}
}
return clazz;
}

When we see a Flex Exception, we show the message to the end user. If for some reason we have an unplanned error in Java, like NullPointerException, a message is displayed to the end user: ‘Error! Please try again. If this issue persists, contact the system administrator’. A generic message to handle all unplanned errors.

5. What if the app needs to DO something special with certain Java Exceptions? Say the user has just entered 3 password attempts, and I want to be able to handle locking their account through an exception.

  • Create a new class in Java called UserLockedException that extends FlexException
  • Create a matching actionscript UserLockedException.
  • Add code in the flex fault handler to look for a UserLockedException, when the fault handler sees it, it can still display a message to the end user, but now flex knows to take a different code path to complete the process.
  • Conclusion

    It is pretty simple with BlazeDS to create a base exception handling system. Depending on project needs, this approach can easily be modified or enhanced. In flex, it is important to setup a strong exception handling that accounts for both planned and unplanned exceptions.

    8 Comments

    • 1. Alok B replies at 20th August 2009, 12:47 am :

      Thanks, I got to understand exception handling in flex a little better.

    • 2. Nuno Chumbo replies at 9th March 2010, 8:00 am :

      Hi there, i’m a litle confused, since i’m newbie, with “… and put the usual RemoteObject tag in their so …”.
      Can you explain me how do i do that?
      Thanks

    • 3. Brian Telintelo replies at 10th March 2010, 7:58 am :

      This is like

      [Bindable]
      [RemoteClass(alias=”com.mypackage.FlexException”)]

    • 4. flexx0r replies at 27th April 2010, 3:06 am :

      retarded code.
      unused variables: cname, instance.

    • 5. flexx0r replies at 27th April 2010, 3:17 am :

      unused variables: cname, instance, fault (method parameter).
      unknown variable - myFault

      i thought that “myFault” is supposed to be “fault”..
      but even in this case it doesnt work.

      private function onFault(event : FaultEvent) : void {
      handleException(event.fault);
      }
      public function handleException(fault : Fault) : void {
      var msg : String = fault.faultString;
      var clazz : Class = getExceptionClass(fault);
      trace(clazz);
      //var instance : Error = null;
      if (clazz != null) {
      Alert.show(msg, ‘Error’);
      }
      else {
      Alert.show(’Error! Please try again. If this issue persists, contact the system administrator’);
      }
      }

      public function getExceptionClass(fault : Fault) : Class {
      var clazz : Class = null;
      var index : int = fault.faultString.indexOf(”SocialException”);
      if (index != -1){
      //var cname : String = fault.faultString.substr(0, index - 1);
      try{
      clazz = getDefinitionByName(”modules.contactmanager.model.vo.SocialException”) as Class;
      }
      catch (e : ReferenceError){
      }
      }
      return clazz;
      }

      and here is the exception class, linked with a matching server-side class:

      package ru.synchronet.modules.contactmanager.model.vo {
      [RemoteClass(alias=”web.app.controllers.social.SocialException”)]
      public class SocialException {
      public var type : Number;
      }
      }

      any idea why?

    • 6. flexx0r replies at 27th April 2010, 3:21 am :

      omg, forgot to delete “ru.synchronet.” in “package ru.synchronet.modules.contactmanager.model.vo”

    • 7. flexx0r replies at 27th April 2010, 3:55 am :

      solved it myself.. its pretty much simple, just
      if(event.fault.rootCause is SocialException){ do specific actions }
      else{ show general error popup }

      delete all my posts above pls :P

    • 8. Shah Al replies at 16th June 2010, 11:32 am :

      Can you please publish the matching class in actionscript for “Flex Exception”?
      I tried creating an equivalent class in as3 , but Flex complains “unable to find RunTimeException”. If i remove it, then
      “TypeError: Error #1034: Type Coercion failed: cannot convert” .

      Not sure what is wrong. Any help is appreciated.

    Leave a comment

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>