Handling errors

Overview

Traditionally, you're left to fend for yourself when it come to catching errors in JSFL. Along with the poor IDE and limited tools, handling errors and debugging scripts could be a complete nightmare.

Of course, no one wants their script to have errors when someone else is using it, but more importantly for you in the development process is actually understanding context in which the error message was provided, and tracking down the actual code which was ultimately responsible for the error in the first place.

Luckily, xJSFL has some pretty nifty tools built in to help you track down those annoying bugs.

try/catch & xjsfl.debug.stack()

JavaScript has the try/catch construct, so your first port of call should be to wrap your code within a try/catch block and do something meaningful with the error:

try
{
    // your code
}
catch(error)
{
    // do something with the error
}

Whilst this is fine in theory, what exactly are you supposed to do with the error? Well, in the case of xJSFL, you should pass it to the xjsfl.debug.error() method, which will print the full error stack to the Output panel:

try
{
    XUL.create('this: is meaningless');
}
catch(error)
{
    xjsfl.debug.error(error);
}
Error: XUL.add(): Undefined control type "this"
----------------------------------------------------------------------------------------------------

	0 > ("XUL.add(): Undefined control type \"this\"")

		line: 883
		file: xjsfl.jsfl
		path: xJSFL/core/jsfl/libraries/

	1 > ("this: is meaningless")

		line: 1185
		file: xul.jsfl
		path: xJSFL/core/jsfl/libraries/

	2 > ("this: is meaningless")

		line: 107
		file: xul.jsfl
		path: xJSFL/core/jsfl/libraries/

	3 > ("this: is meaningless")

		line: 130
		file: xul.jsfl
		path: xJSFL/core/jsfl/libraries/

	4 > 

		line: 47
		file: temp.jsfl
		path: xJSFL/core/jsfl/temp/

xjsfl.debug.func()

try/catch is great, but what if you're feeling a bit on the lazy side? Well, if you have a function, or a method on an object you want to test, you can just pass it to xjsfl.debug.func():

xjsfl.debug.func(func);
xjsfl.debug.func(object.method, [1, 2, 3, 4]);

The error stack will be printed to the listener as before.

xjsfl.debug.file()

Loading files, or more specifically running files, in Flash is a principle cause of frustration due to the little-known fact that syntax or runtime errors inside files that are run with fl.runScript() will fail silently!

To get around this, xJSFL provides functionality via xjsfl.debug.file() which wraps the native fl.runScript() and instead of directly running the file, loads the contents of the file then eval()s it, which has the result of outputting any errors to the Output panel. Whilst this technique should never be used in production code, it can be useful for tracking down otherwise frustratingly silent errors.

To trigger this functionality load files via xjsfl.debug.file(), or turn debugging on explicitly before any file loading:

// option 1: load and debug only this file, and any files that it loads
xjsfl.debug.file(uri);
// option 2: turn on debugging for all file loads
xjsfl.debug.state = true;

Next steps

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>