Skip to content
Johan Hernandez edited this page Nov 23, 2011 · 4 revisions

Handling Errors

fire.js supports error handling for expressions.

using @try and @catch

@try will execute and return the input, if any error occurs it will silently catch the error and bypass so @catch, @returnError or @clearError can work properly.

The following fire.js app raises an error using @raiseError but it also handles it properly using @catch:

{
	"name": "Examples.ErrorCatching",
	"json": {
		"@try": {
			"@raiseError": "This is an error, we are so screwed!"
		},
		"@catch": {
			"whatHappended": {
				"@get(CurrentError.error)": null
			},
			"howAreWeDoing": "We had some trouble but we are ok now!"
		}
	}
}

Download examples/Examples.ErrorCatching.fjson and execute to see the results:

$ firejs Examples.ErrorCatching.fjson
{"whatHappended":"This is an error, we are so screwed!","howAreWeDoing":"We had some trouble but we are ok now!"}

Notice that @catch exposes a variable called "CurrentError" to the input so we know what the error was, it's an instance of a type called RuntimeError and it's member error contains the original error.

@catch can also accept a hint so you can add a prefix to CurrentError and avoid conflicts with any surrounding @catch.

Considerations when using CurrentError

CurrentError actually contains an instance of a type called RuntimeError, it contains members with circular references that are unsafe to be "stringified" by JSON and will cause errors like this:

TypeError: Converting circular structure to JSON
at Object.stringify (native)
...

The only member that can be safely converted to json is error. If what you want is unconditionally return the real error you should use @returnError instead.

Note: It's highly discouraged to return CurrentError as the result of any expression under any circumstance.

using @returnError

This expression will return the real cause of the error(not the instance of RuntimeError).

Example:

{
	"name": "Examples.ReturningErrors",
	"json": {
		"@try": {
			"@raiseError": "This is an error, we are so screwed!"
		},
		"@returnError": null
	}
}

Download examples/Examples.ReturningErrors.fjson and execute to see the results:

$ firejs Examples.ReturningErrors.fjson
"This is an error, we are so screwed!"

using @clearError

This expression will clear any error found.

Example:

{
	"name": "Examples.ClearingErrors",
	"json": {
		"@return": "Boring... no errors happening around here",
		"@try": {
			"@raiseError": "This is an error, we are so screwed!"
		},
		"@clearError": null,
		"@catch": "Whoa... This is an error!"
	}
}

Download examples/Examples.ClearingErrors.fjson and execute to see the results:

$ firejs Examples.ClearingErrors.fjson
"Boring... no errors happening around here"

Thanks for Reading. Please take your time to read other tutorials.

Contributors

If you find any error in this tutorial(misspelling errors, syntax errors, etc) please send me an email with your corrections, I'll be more than happy to add you in this list.