15 has foo => ( is => "ro" );
19 metaclass => "Moose::Meta::Class",
20 error_class => "Moose::Error::Croak",
24 has foo => ( is => "ro" );
29 extends 'Moose::Object', 'Moose::Error::Default';
31 has message => ( isa => "Str", is => "ro" );
32 has attr => ( isa => "Moose::Meta::Attribute", is => "ro" );
33 has method => ( isa => "Moose::Meta::Method", is => "ro" );
34 has metaclass => ( isa => "Moose::Meta::Class", is => "ro" );
35 has data => ( is => "ro" );
36 has line => ( isa => "Int", is => "ro" );
37 has file => ( isa => "Str", is => "ro" );
38 has last_error => ( isa => "Any", is => "ro" );
43 metaclass => "Moose::Meta::Class",
44 error_class => "Baz::Error",
48 has foo => ( is => "ro" );
52 sub blah { $line = __LINE__; shift->foo(4) }
59 ok( my $e = $@, "got some error" );
69 my $e = create_error( Foo->new );
70 ok( !ref( $e->{error} ), "error is a string" );
71 like( $e->{error}, qr/line $e->{line}\n.*\n/s, "confess" );
75 my $e = create_error( Bar->new );
76 ok( !ref( $e->{error} ), "error is a string" );
77 like( $e->{error}, qr/line $e->{line}\.?$/s, "croak" );
81 my $e = create_error( my $baz = Baz->new );
82 isa_ok( $e->{error}, "Baz::Error" );
83 unlike( $e->{error}->message, qr/line $e->{line}/s,
84 "no line info, just a message" );
85 isa_ok( $e->{error}->metaclass, "Moose::Meta::Class", "metaclass" );
86 is( $e->{error}->metaclass, Baz->meta, "metaclass value" );
87 isa_ok( $e->{error}->attr, "Moose::Meta::Attribute", "attr" );
88 is( $e->{error}->attr, Baz->meta->get_attribute("foo"), "attr value" );
89 isa_ok( $e->{error}->method, "Moose::Meta::Method", "method" );
90 is( $e->{error}->method, Baz->meta->get_method("foo"), "method value" );
91 is( $e->{error}->line, $e->{line}, "line attr" );
92 is( $e->{error}->file, $e->{file}, "file attr" );
93 is_deeply( $e->{error}->data, [ $baz, 4 ], "captured args" );
94 like( $e->{error}->last_error, qr/Blah/, "last error preserved" );
96 } 'Foo', 'Bar', 'Baz';
111 Moose::Util::MetaRole::apply_metaroles(
113 class_metaroles => { class => ['Role::Foo'] },
118 package Baz::Sub::Sub;
120 metaclass => 'Moose::Meta::Class',
121 error_class => 'Moose::Error::Croak',
125 ::isnt( ::exception { extends 'Baz::Sub' }, undef, 'error_class is included in metaclass compatibility checks' );
132 metaclass => 'Moose::Meta::Class',
133 error_class => 'Moose::Error::Croak',
138 Moose::Util::MetaRole::apply_metaroles(
140 class_metaroles => { class => ['Role::Foo'] },
144 ok( Foo::Sub->meta->error_class->isa('Moose::Error::Croak'),
145 q{Foo::Sub's error_class still isa Moose::Error::Croak} );
148 package Foo::Sub::Sub;
151 ::is( ::exception { extends 'Foo::Sub' }, undef, 'error_class differs by role so incompat is handled' );
153 Moose::Util::MetaRole::apply_metaroles(
155 class_metaroles => { error => ['Role::Foo'] },
159 ok( Foo::Sub::Sub->meta->error_class->meta->does_role('Role::Foo'),
160 q{Foo::Sub::Sub's error_class does Role::Foo} );
161 ok( Foo::Sub::Sub->meta->error_class->isa('Moose::Error::Croak'),
162 q{Foo::Sub::Sub's error_class now subclasses Moose::Error::Croak} );
165 package Quux::Default;
168 has foo => (is => 'ro');
169 sub bar { shift->foo(1) }
174 use metaclass 'Moose::Meta::Class', error_class => 'Moose::Error::Croak';
177 has foo => (is => 'ro');
178 sub bar { shift->foo(1) }
182 package Quux::Confess;
183 use metaclass 'Moose::Meta::Class', error_class => 'Moose::Error::Confess';
186 has foo => (is => 'ro');
187 sub bar { shift->foo(1) }
190 sub stacktrace_ok (&) {
191 local $Test::Builder::Level = $Test::Builder::Level + 1;
194 my @lines = split /\n/, $@;
195 cmp_ok(scalar(@lines), '>', 1, "got a stacktrace");
198 sub stacktrace_not_ok (&) {
199 local $Test::Builder::Level = $Test::Builder::Level + 1;
202 my @lines = split /\n/, $@;
203 cmp_ok(scalar(@lines), '==', 1, "didn't get a stacktrace");
207 my $default = Quux::Default->new;
208 my $croak = Quux::Croak->new;
209 my $confess = Quux::Confess->new;
211 is($default->meta->error_class, 'Moose::Error::Default');
212 is($croak->meta->error_class, 'Moose::Error::Croak');
213 is($confess->meta->error_class, 'Moose::Error::Confess');
216 local $ENV{MOOSE_ERROR_STYLE};
217 stacktrace_ok { $default->bar };
218 stacktrace_not_ok { $croak->bar };
219 stacktrace_ok { $confess->bar };
223 local $ENV{MOOSE_ERROR_STYLE} = 'croak';
224 stacktrace_not_ok { $default->bar };
225 stacktrace_not_ok { $croak->bar };
226 stacktrace_ok { $confess->bar };
230 local $ENV{MOOSE_ERROR_STYLE} = 'confess';
231 stacktrace_ok { $default->bar };
232 stacktrace_not_ok { $croak->bar };
233 stacktrace_ok { $confess->bar };
235 } 'Quux::Default', 'Quux::Croak', 'Quux::Confess';