| JE documentation | Contained in the JE distribution. |
JE::Object::Error - JavaScript Error object class
use JE::Object::Error;
# Somewhere in code called by an eval{}
die new JE::Object::Error $global, "(Error message here)";
# Later:
$@->prop('message'); # error message
$@->prop('name'); # 'Error'
"$@"; # 'Error: ' plus the error message
This class implements JavaScript Error objects for JE. This is the base class for all JavaScript's native error objects. (See SEE ALSO, below.)
See JE::Types for descriptions of most of the methods. Only what is specific to JE::Object::Error is explained here.
The value method returns the string 'Error: ' followed by the error
message. 'Error' will be replaced with the class name (the result of
calling
->class) for subclasses.
The new_constructor method (see JE::Object for details) does not work
in subclasses. If you create a new_constructor method in your own
subclass of JE::Object::Error, call
$class->JE::Object::new_constructor instead of using SUPER.
| JE documentation | Contained in the JE distribution. |
package JE::Object::Error; our $VERSION = '0.042'; use strict; use warnings; our @ISA = 'JE::Object'; require JE::Object; require JE::String; # ~~~ Need to add support for line number, script name, etc., or perhaps # just a reference to the corresponding JE::Code object.
sub new { my($class, $global, $val) = @_; my $self = $class->SUPER::new($global, { prototype => $global->prototype_for(class $class) || $global->prop(class $class)->prop('prototype') }); $self->prop({ dontenum => 1, name => 'message', value => JE::String->_new($global, $val), }); $self; } sub value { $_[0]->method('toString')->value } sub class { 'Error' } sub new_constructor { shift->SUPER::new_constructor(shift, sub { __PACKAGE__->new(@_); }, sub { my $proto = shift; my $global = $$proto->{global}; $global->prototype_for('Error',$proto); bless $proto, __PACKAGE__; $proto->prop({ name => 'toString', value => JE::Object::Function->new({ scope => $global, name => 'toString', length => 0, function_args => ['this'], function => sub { my $self = shift; JE::String->_new( $$$self{global}, $self->class . ': ' . $self->prop( 'message' ) ); } }), dontenum => 1, }); $proto->prop({ name => 'name', value => JE::String->_new($global, 'Error'), dontenum => 1, }); $proto->prop({ name => 'message', value => JE::String->_new($global, 'Unknown Error'), dontenum => 1, }); }, ); } return "a true value";