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 | |
23 | sub create_error_croak { |
24 | my ( $self, @args ) = @_; |
41daa828 |
25 | $self->_create_error_carpmess( @args ); |
bf6fa6b3 |
26 | } |
27 | |
28 | sub create_error_confess { |
29 | my ( $self, @args ) = @_; |
30 | $self->_create_error_carpmess( @args, longmess => 1 ); |
31 | } |
32 | |
33 | sub _create_error_carpmess { |
34 | my ( $self, %args ) = @_; |
35 | |
36 | my $carp_level = 3 + ( $args{depth} || 1 ); |
41daa828 |
37 | local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though |
bf6fa6b3 |
38 | |
39 | my @args = exists $args{message} ? $args{message} : (); |
40 | |
41 | if ( $args{longmess} || $Carp::Verbose ) { |
42 | local $Carp::CarpLevel = ( $Carp::CarpLevel || 0 ) + $carp_level; |
43 | return Carp::longmess(@args); |
41daa828 |
44 | } else { |
45 | return Carp::ret_summary($carp_level, @args); |
bf6fa6b3 |
46 | } |
47 | } |
48 | |
9c1bf11e |
49 | 1; |
bf6fa6b3 |
50 | |
ad46f524 |
51 | # ABSTRACT: L<Carp> based error generation for Moose. |
52 | |
bf6fa6b3 |
53 | __END__ |
54 | |
55 | =pod |
56 | |
bf6fa6b3 |
57 | =head1 DESCRIPTION |
58 | |
59 | This class implements L<Carp> based error generation. |
60 | |
1b27b2a6 |
61 | The default behavior is like L<Moose::Error::Confess>. To override this to |
62 | default to L<Moose::Error::Croak>'s behaviour on a system wide basis, set the |
63 | MOOSE_ERROR_STYLE environment variable to C<croak>. The use of this |
64 | environment variable is considered experimental, and may change in a future |
65 | release. |
bf6fa6b3 |
66 | |
67 | =head1 METHODS |
68 | |
69 | =over 4 |
70 | |
ad3882b5 |
71 | =item B<< Moose::Error::Default->new(@args) >> |
bf6fa6b3 |
72 | |
cef1e197 |
73 | Create a new error. Delegates to C<create_error_confess> or |
74 | C<create_error_croak>. |
bf6fa6b3 |
75 | |
ad3882b5 |
76 | =item B<< $error->create_error_confess(@args) >> |
bf6fa6b3 |
77 | |
ad3882b5 |
78 | =item B<< $error->create_error_croak(@args) >> |
bf6fa6b3 |
79 | |
80 | Creates a new errors string of the specified style. |
81 | |
82 | =back |
83 | |
84 | =cut |
85 | |
86 | |