-
Notifications
You must be signed in to change notification settings - Fork 1
Error Handling
priest.js supports error handling for expressions.
@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 priest 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.priest.json and execute to see the results:
$ priest Examples.ErrorCatching.priest.json
{"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 an object called RuntimeError and it's member error
contains the real cause.
@catch
can also accept a hint so you can add a prefix to CurrentError and avoid conflicts with any surrounding @catch
.
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.
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.priest.json and execute to see the results:
$ priest Examples.ReturningErrors.priest.json
"This is an error, we are so screwed!"
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.priest.json and execute to see the results:
$ priest Examples.ClearingErrors.priest.json
"Boring... no errors happening around here"
Thanks for Reading. Please take your time to read other tutorials.
- Johan(author) - [email protected]
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.