Since version 2.02 Moose started to issue warnings when a class using a
non-standard error-class is made immutable.
Furthermore exceptions from classes that are not immutable have wrong
linenumbers and package names.
See attached testcases.
Subject: | errorclass.t |
#!/usr/bin/env perl
use Test::More tests => 8+1;
use Test::NoWarnings;
use strict;
use warnings;
{
package mytest::Exception;
use Moose;
has 'error' => ( is => 'ro', isa => 'Str', required => 1 );
has [qw(line file package)] => ( is => 'ro', required => 1 );
sub throw {
my ($self) = @_;
die $self;
}
}
{
package mytest::Error;
use parent qw(Moose::Error::Default);
sub new {
my ( $self, @args ) = @_;
$self->create_error_exception(@args)->throw;
}
sub create_error_exception {
my ( $self, %params ) = @_;
my $exception = mytest::Exception->new(
error => $params{message},
line => $params{line},
file => $params{file},
package => $params{pack},
);
return $exception;
}
}
{
package mytest::Class;
use Moose;
__PACKAGE__->meta->error_class("mytest::Error");
has 'test1' => ( is => 'rw', required => 1 );
__PACKAGE__->meta->make_immutable;
}
{
package mytest::ClassMutable;
use Moose;
__PACKAGE__->meta->error_class("mytest::Error");
has 'test1' => ( is => 'rw', required => 1 );
}
{
eval {
package mytest::Test;
# line 42
mytest::Class->new
};
my $error = $@;
isa_ok($error,'mytest::Exception','Is exception object');
is($error->error,'Attribute (test1) is required','Message ok');
is($error->package,'mytest::Test','Package ok');
is($error->line,42,'Line ok');
}
{
eval {
package mytest::TestMutable;
# line 42
mytest::ClassMutable->new
};
my $error = $@;
isa_ok($error,'mytest::Exception','Is exception object');
is($error->error,'Attribute (test1) is required','Message ok');
is($error->package,'mytest::TestMutable','Package ok');
is($error->line,42,'Line ok');
}