Require Dist::Zilla 4.200016+
[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 Moose::Error::Util;
10
11 use base 'Class::MOP::Object';
12
13 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
14
15 sub new {
16     my ( $self, @args ) = @_;
17     # can't use Moose::Error::Util::create_error here because that would break
18     # inheritance. we don't care about that for the inlined version, because
19     # the inlined versions are explicitly not inherited.
20     if (defined $ENV{MOOSE_ERROR_STYLE} && $ENV{MOOSE_ERROR_STYLE} eq 'croak') {
21         $self->create_error_croak( @args );
22     }
23     else {
24         $self->create_error_confess( @args );
25     }
26 }
27
28 sub _inline_new {
29     my ( $self, %args ) = @_;
30
31     my $depth = ($args{depth} || 0) - 1;
32     return 'Moose::Error::Util::create_error('
33       . 'message => ' . $args{message} . ', '
34       . 'depth   => ' . $depth         . ', '
35   . ')';
36 }
37
38 sub create_error_croak {
39     my ( $self, @args ) = @_;
40     return Moose::Error::Util::create_error_croak(@args);
41 }
42
43 sub create_error_confess {
44     my ( $self, @args ) = @_;
45     return Moose::Error::Util::create_error_confess(@args);
46 }
47
48 1;
49
50 # ABSTRACT: L<Carp> based error generation for Moose.
51
52 __END__
53
54 =pod
55
56 =head1 DESCRIPTION
57
58 This class implements L<Carp> based error generation.
59
60 The default behavior is like L<Moose::Error::Confess>. To override this to
61 default to L<Moose::Error::Croak>'s behaviour on a system wide basis, set the
62 MOOSE_ERROR_STYLE environment variable to C<croak>. The use of this
63 environment variable is considered experimental, and may change in a future
64 release.
65
66 =head1 METHODS
67
68 =over 4
69
70 =item B<< Moose::Error::Default->new(@args) >>
71
72 Create a new error. Delegates to C<create_error_confess> or
73 C<create_error_croak>.
74
75 =item B<< $error->create_error_confess(@args) >>
76
77 =item B<< $error->create_error_croak(@args) >>
78
79 Creates a new errors string of the specified style.
80
81 =back
82
83 =cut
84
85