1 package Moose::Error::Default;
7 use Class::MOP::MiniTrait;
9 use base 'Class::MOP::Object';
11 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
14 my ( $self, @args ) = @_;
15 if (defined $ENV{MOOSE_ERROR_STYLE} && $ENV{MOOSE_ERROR_STYLE} eq 'croak') {
16 $self->create_error_croak( @args );
19 $self->create_error_confess( @args );
23 sub create_error_croak {
24 my ( $self, @args ) = @_;
25 $self->_create_error_carpmess( @args );
28 sub create_error_confess {
29 my ( $self, @args ) = @_;
30 $self->_create_error_carpmess( @args, longmess => 1 );
33 sub _create_error_carpmess {
34 my ( $self, %args ) = @_;
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
39 my @args = exists $args{message} ? $args{message} : ();
41 if ( $args{longmess} || $Carp::Verbose ) {
42 local $Carp::CarpLevel = ( $Carp::CarpLevel || 0 ) + $carp_level;
43 return Carp::longmess(@args);
45 return Carp::ret_summary($carp_level, @args);
51 # ABSTRACT: L<Carp> based error generation for Moose.
59 This class implements L<Carp> based error generation.
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
71 =item B<< Moose::Error::Default->new(@args) >>
73 Create a new error. Delegates to C<create_error_confess> or
74 C<create_error_croak>.
76 =item B<< $error->create_error_confess(@args) >>
78 =item B<< $error->create_error_croak(@args) >>
80 Creates a new errors string of the specified style.