Custom Java Exceptions, BlazeDS and Flex
April 7th, 2011 by Dave TurnerHere is a quick and dirty way to get flex to play well with Custom Java Exceptions
Custom Java exception
A Simple Java exception that adds a new field called customDetails (via a Bean Property)
public class myCustomException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String details;
public CampaignSaveException(String arg0) {
super(arg0);
details = arg0;
}
public String getCustomDetails(){
return details;
}
}
When Java throws this exception during a remote call (i.e..)
throw new myCustomException ("Details for the error message");
I can use this method to identify that this is in fact a myCustomException exception by looking for properties relevant to that class. This gives me an option to do some conditional logic when handling exceptions.
Note the usage of the “Bean property” where getCustomDetails() is set in the Java class but referenced in the front end as errorMessage.rootCause.customDetails.
private function onSaveError(event:FaultEvent):void
{
var errorMessage:ErrorMessage = event.message as ErrorMessage;
if(errorMessage.rootCause.customDetails != null)
Alert.show(errorMessage.rootCause.customDetails, "", Alert.OK);
else{
Alert.show("Generic Error Message", "", Alert.OK);
}
}
I believe there are more advanced methods than looking for a “bean property” (maybe casting?) but as the title suggests this is a quick and dirty example. Looking over the BlazeDS documentation
http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=serialize_data_2.html.
It doesn’t look like BlazeDS will map Custom Java Exceptions to Flex Exceptions .. So i think there’s always going to be a little bit of hackery to any implementation of passing and handling exceptions between Java and Flex.