User Guide

Example: CustomErrors application 281
The ApplicationError classs constructor method is as follows:
public function ApplicationError()
{
messages =
<errors>
<error code="9000">
<![CDATA[Employee must be assigned to a cost center.]]>
</error>
<error code="9001">
<![CDATA[Employee must be assigned to only one cost center.]]>
</error>
<error code="9002">
<![CDATA[Employee must have one and only one SSN.]]>
</error>
<error code="9999">
<![CDATA[The application has been stopped.]]>
</error>
</errors>;
}
Each error node in the XML object contains a unique numeric code and an error message.
Error messages can be easily looked up by their error code using E4X, as seen in the following
getMessageText() method:
public function getMessageText(id:int):String
{
var message:XMLList = messages.error.(@code == id);
return message[0].text();
}
The getMessageText() method takes a single integer argument, id, and returns a string.
The
id argument is the error code for the error to look up. For example, passing an id of
9001 retrieves the error saying that employees must be assigned to only one cost center. If
more than one error has the same error code, ActionScript returns the error message only for
the first result found (
message[0] in the returned XMLList object).
The next method in this class,
getTitle(), doesnt take any parameters and returns a string
value that contains the error ID for this specific error. This value is used in the Alert
component’s title to help you easily identify the exact error that occurred during validation of
the XML packet. The following excerpt shows the
getTitle() method:
public function getTitle():String
{
return "Error #" + id;
}