++ed by:
1 non-PAUSE user
Author image Salvador Ortiz


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 $@.

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

The following shows an example:

    eval {
            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($@).

So the code above is seen by JavaScript as if throw new Error('something fails'); was executed.

new($message, $fileName)
new($message, $fileName, $lineNumber)

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.

If called outside, it dies with the error "Not in a javascript context".

Instance properties

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.

Instance methods

The following methods are simple perl wrappers over the properties above, use when you like more methods than properties.

message ( )

The cause of the exception.

file ( )

The name of the file that the caused the exception.

line ( )

The line number in the file that caused the exception.

as_string ( )

A stringification of the exception in the format $message at $file line $line

stacktrace ( )

Returns the stacktrace for the exception as a list of hashrefs containing function, file and lineno.


This class overloads stringification an will return the result from the method as_string.