JSPL::Error - Encapsulates errors thrown from JavaScript
JavaScript runtime errors result in new Error objects being created and thrown. When not handled in JavaScript, those objects will arrive to perl space when are wrapped as an instance of JSPL::Error and stored in $@.
Error
$@
What happens next depends on the value of the option "RaiseExceptions" in JSPL::Context.
If TRUE perl generates a fatal but trappable exception.
If FALSE the operation returns undef
undef
The following shows an example:
eval { $ctx->eval(q{ throw new Error("Whoops!"); // Synthesize a runtime error }); } if($@) { print $@->toString(); # 'Error: Whoops!' }
JSPL::Error inherits from JSPL::Object so you use them as any other JavaScript Object.
In Perl you can create new JSPL::Error instances, useful when you need to throw an exception from a perl function called from JavaScript:
die(JSPL::Error->new('something fails'));
In fact, when you die in perl land inside code that is being called from JavaScript and if the error (in $@) is a simple perl string, it will be converted to an <Error> instance with the equivalent to JSPL::Error->new($@).
die
JSPL::Error->new($@)
So the code above is seen by JavaScript as if throw new Error('something fails'); was executed.
throw new Error('something fails');
If inside perl code that is called from JavaScript, new(...) will constructs a new JavaScript Error instance, wrap it in a JSPL::Error object and return it.
new(...)
If called outside, it dies with the error "Not in a javascript context".
Error instances in JavaScript have the following properties.
Error message
Error Name
Path to file that raised this error.
Line number in file that raised this error.
Stack trace.
The following methods are simple perl wrappers over the properties above, use when you like more methods than properties.
The cause of the exception.
The name of the file that the caused the exception.
The line number in the file that caused the exception.
A stringification of the exception in the format $message at $file line $line
$message at $file line $line
Returns the stacktrace for the exception as a list of hashrefs containing function, file and lineno.
function
file
lineno
This class overloads stringification an will return the result from the method as_string.
as_string
To install JSPL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSPL
CPAN shell
perl -MCPAN -e shell install JSPL
For more information on module installation, please visit the detailed CPAN module installation guide.