The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

JSP::Error - Encapsulates errors thrown from javascript

DESCRIPTION

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 JSP::Error and stored in $@.

What happens next depends on the value of the option "RaiseExceptions" in JSP::Context.

  • If TRUE perl generates a fatal but trappable exeption.

  • If FALSE the operation returns undef

The following shows an example:

    eval {
	$ctx->eval(q{
	    throw new Error("Whoops!"); // Synthesize a runtime error
	});
    }
    if($@) {
	print $@->toString(); # 'Error: Whoops!'
    }
	    

PERL INTERFACE

JSP::Error inherits from JSP::Object so you use them as any other javascript Object.

Constructor

In Perl you can create new JSP::Error instances, usefull when you need to throw an exception from a perl land function called from javascript:

die(JSP::Error->new('something fails'));

In fact, when you die in perl land inside a function 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 JSP::Error->new($@).

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

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

If inside perl code that is called from javascript, new(...) will contructs a new javascript Error instance, wrap it in a JSP::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.

message

Error message

name

Error Name

fileName

Path to file that raised this error.

lineNumber

Line number in file that raised this error.

stack

Stack trace.

Instance methods

The following methods are simple perl wrappers over the properties above used 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.

OVERLOADED OPERATIONS

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