13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Handling errors<br />

You can add additional checks to the validateEmployeeXML() method—for example, to <strong>en</strong>sure that the ssn node<br />

contains a valid number, or that the employee has at least one phone number and e-mail address defined, and that both<br />

values are valid. You can also modify the XML so that each employee has a unique ID and specifies the ID of their<br />

manager.<br />

Defining the ApplicationError class<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The ApplicationError class serves as the base class for both the FatalError and WarningError classes. The<br />

ApplicationError class ext<strong>en</strong>ds the Error class, and defines its own custom methods and properties, including defining<br />

an error ID, severity, and an XML object that contains the custom error codes and messages. This class also defines<br />

two static constants that are used to define the severity of each error type.<br />

The ApplicationError class’s constructor method is as follows:<br />

public function ApplicationError()<br />

{<br />

messages =<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

;<br />

}<br />

Each error node in the XML object contains a unique numeric code and an error message. Error messages can be easily<br />

looked up by their error code using E4X, as se<strong>en</strong> in the following getMessageText() method:<br />

public function getMessageText(id:int):String<br />

{<br />

var message:XMLList = messages.error.(@code == id);<br />

return message[0].text();<br />

}<br />

The getMessageText() method takes a single integer argum<strong>en</strong>t, id, and returns a string. The id argum<strong>en</strong>t is the error<br />

code for the error to look up. For example, passing an id of 9001 retrieves the error saying that employees must be<br />

assigned to only one cost c<strong>en</strong>ter. If more than one error has the same error code, ActionScript returns the error<br />

message only for the first result found (message[0] in the returned XMLList object).<br />

The next method in this class, getTitle(), doesn’t take any parameters and returns a string value that contains the<br />

error ID for this specific error. This value is used to help you easily id<strong>en</strong>tify the exact error that occurred during<br />

validation of the XML packet. The following excerpt shows the getTitle() method:<br />

public function getTitle():String<br />

{<br />

return "Error #" + id;<br />

}<br />

Last updated 6/6/2012<br />

73

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!