Commit | Line | Data |
bf6fa6b3 |
1 | package Moose::Error::Default; |
2 | |
c0e91e5f |
3 | use strict; |
4 | use warnings; |
5 | |
41daa828 |
6 | use Carp::Heavy; |
68567b15 |
7 | use Class::MOP::MiniTrait; |
41daa828 |
8 | |
4f5ca7c5 |
9 | use base 'Class::MOP::Object'; |
10 | |
68567b15 |
11 | Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait'); |
41daa828 |
12 | |
bf6fa6b3 |
13 | sub new { |
14 | my ( $self, @args ) = @_; |
692a93f6 |
15 | if (defined $ENV{MOOSE_ERROR_STYLE} && $ENV{MOOSE_ERROR_STYLE} eq 'croak') { |
aeef093c |
16 | $self->create_error_croak( @args ); |
17 | } |
18 | else { |
19 | $self->create_error_confess( @args ); |
20 | } |
bf6fa6b3 |
21 | } |
22 | |
bcc04ae1 |
23 | sub _inline_new { |
72c4fdf1 |
24 | my ( $self, %args ) = @_; |
bcc04ae1 |
25 | |
72c4fdf1 |
26 | my $depth = ($args{depth} || 0) - 1; |
27 | return $self . '->new(' |
28 | . 'message => ' . $args{message} . ', ' |
29 | . 'depth => ' . $depth . ', ' |
30 | . ')'; |
bcc04ae1 |
31 | } |
32 | |
bf6fa6b3 |
33 | sub create_error_croak { |
34 | my ( $self, @args ) = @_; |
41daa828 |
35 | $self->_create_error_carpmess( @args ); |
bf6fa6b3 |
36 | } |
37 | |
38 | sub create_error_confess { |
39 | my ( $self, @args ) = @_; |
40 | $self->_create_error_carpmess( @args, longmess => 1 ); |
41 | } |
42 | |
43 | sub _create_error_carpmess { |
44 | my ( $self, %args ) = @_; |
45 | |
72c4fdf1 |
46 | my $carp_level = 4 + ( $args{depth} || 0 ); |
41daa828 |
47 | local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though |
bf6fa6b3 |
48 | |
49 | my @args = exists $args{message} ? $args{message} : (); |
50 | |
51 | if ( $args{longmess} || $Carp::Verbose ) { |
52 | local $Carp::CarpLevel = ( $Carp::CarpLevel || 0 ) + $carp_level; |
53 | return Carp::longmess(@args); |
41daa828 |
54 | } else { |
55 | return Carp::ret_summary($carp_level, @args); |
bf6fa6b3 |
56 | } |
57 | } |
58 | |
9c1bf11e |
59 | 1; |
bf6fa6b3 |
60 | |
ad46f524 |
61 | # ABSTRACT: L<Carp> based error generation for Moose. |
62 | |
bf6fa6b3 |
63 | __END__ |
64 | |
65 | =pod |
66 | |
bf6fa6b3 |
67 | =head1 DESCRIPTION |
68 | |
69 | This class implements L<Carp> based error generation. |
70 | |
1b27b2a6 |
71 | The default behavior is like L<Moose::Error::Confess>. To override this to |
72 | default to L<Moose::Error::Croak>'s behaviour on a system wide basis, set the |
73 | MOOSE_ERROR_STYLE environment variable to C<croak>. The use of this |
74 | environment variable is considered experimental, and may change in a future |
75 | release. |
bf6fa6b3 |
76 | |
77 | =head1 METHODS |
78 | |
79 | =over 4 |
80 | |
ad3882b5 |
81 | =item B<< Moose::Error::Default->new(@args) >> |
bf6fa6b3 |
82 | |
cef1e197 |
83 | Create a new error. Delegates to C<create_error_confess> or |
84 | C<create_error_croak>. |
bf6fa6b3 |
85 | |
ad3882b5 |
86 | =item B<< $error->create_error_confess(@args) >> |
bf6fa6b3 |
87 | |
ad3882b5 |
88 | =item B<< $error->create_error_croak(@args) >> |
bf6fa6b3 |
89 | |
90 | Creates a new errors string of the specified style. |
91 | |
92 | =back |
93 | |
94 | =cut |
95 | |
96 | |