Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Moose / Error / Default.pm
1 package Moose::Error::Default;
2
3 use strict;
4 use warnings;
5
6 use Carp::Heavy;
7 use Class::MOP::MiniTrait;
8
9 use base 'Class::MOP::Object';
10
11 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
12
13 sub new {
14     my ( $self, @args ) = @_;
15     if (defined $ENV{MOOSE_ERROR_STYLE} && $ENV{MOOSE_ERROR_STYLE} eq 'croak') {
16         $self->create_error_croak( @args );
17     }
18     else {
19         $self->create_error_confess( @args );
20     }
21 }
22
23 sub create_error_croak {
24     my ( $self, @args ) = @_;
25     $self->_create_error_carpmess( @args );
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 );
37     local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
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);
44     } else {
45         return Carp::ret_summary($carp_level, @args);
46     }
47 }
48
49 __PACKAGE__
50
51 # ABSTRACT: L<Carp> based error generation for Moose.
52
53 __END__
54
55 =pod
56
57 =head1 DESCRIPTION
58
59 This class implements L<Carp> based error generation.
60
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.
66
67 =head1 METHODS
68
69 =over 4
70
71 =item B<< Moose::Error::Default->new(@args) >>
72
73 Create a new error. Delegates to C<create_error_confess> or
74 C<create_error_croak>.
75
76 =item B<< $error->create_error_confess(@args) >>
77
78 =item B<< $error->create_error_croak(@args) >>
79
80 Creates a new errors string of the specified style.
81
82 =back
83
84 =cut
85
86