File Coverage

blib/lib/Moose/Exception.pm
Criterion Covered Total %
statement 35 36 97.2
branch 9 10 90.0
condition 8 9 88.8
subroutine 6 7 85.7
pod 1 2 50.0
total 59 64 92.1


line stmt bran cond sub pod time code
1             package Moose::Exception;
2             our $VERSION = '2.2203';
3              
4 186     186   195299 use Moose;
  186         447  
  186         1007  
5 186     186   85336 use Devel::StackTrace 2.03;
  186         553215  
  186         15811  
6              
7             has 'trace' => (
8             is => 'ro',
9             isa => 'Devel::StackTrace',
10             builder => '_build_trace',
11             lazy => 1,
12             documentation => "This attribute is read-only and isa L<Devel::StackTrace>. ".
13             'It is lazy & dependent on $exception->message.'
14             );
15              
16             has 'message' => (
17             is => 'ro',
18             isa => 'Defined',
19             builder => '_build_message',
20             lazy => 1,
21             documentation => "This attribute is read-only and isa Defined. ".
22             "It is lazy and has a default value 'Error'."
23             );
24              
25             use overload(
26             q{""} => 'as_string',
27             bool => sub () { 1 },
28 186         911 fallback => 1,
29 186     186   1286 );
  186         415  
30              
31             sub _build_trace {
32 3689     3689   4925 my $self = shift;
33              
34             # skip frames that are method calls on the exception object, which include
35             # the object itself in the arguments (but Devel::LeakTrace really ought to
36             # be weakening all references in its frames)
37 3689         5107 my $skip = 0;
38 3689         26510 while (my @c = caller(++$skip)) {
39 11085 100 100     92867 last if ($c[3] =~ /^(.*)::new$/ || $c[3] =~ /^\S+ (.*)::new \(defined at /)
      66        
40             && $self->isa($1);
41             }
42 3689         6867 $skip++;
43              
44 3689         83343 Devel::StackTrace->new(
45             message => $self->message,
46             indent => 1,
47             skip_frames => $skip,
48             no_refs => 1,
49             );
50             }
51              
52             sub _build_message {
53 0     0   0 "Error";
54             }
55              
56             sub BUILD {
57 3706     3706 0 5420 my $self = shift;
58 3706         90372 $self->trace;
59             }
60              
61             sub as_string {
62 1631     1631 1 540967 my $self = shift;
63              
64 1631 100       5384 if ( $ENV{MOOSE_FULL_EXCEPTION} ) {
65 5         156 return $self->trace->as_string;
66             }
67              
68 1626         3355 my @frames;
69             my $last_frame;
70 1626         2406 my $in_moose = 1;
71 1626         46954 for my $frame ( $self->trace->frames ) {
72 19141 100 100     841040 if ( $in_moose && $frame->package =~ /^(?:Moose|Class::MOP)(?::|$)/ )
    100          
73             {
74 2193         13332 $last_frame = $frame;
75 2193         3197 next;
76             }
77             elsif ($last_frame) {
78 459         3107 push @frames, $last_frame;
79 459         815 undef $last_frame;
80             }
81              
82 16948         27338 $in_moose = 0;
83 16948         19758 push @frames, $frame;
84             }
85              
86             # This would be a somewhat pathological case, but who knows
87 1626 50       4166 return $self->trace->as_string unless @frames;
88              
89 1626         5736 my $message = ( shift @frames )->as_string( 1, {} ) . "\n";
90 1626         40573 $message .= join q{}, map { $_->as_string( 0, {} ) . "\n" } @frames;
  15781         663624  
91              
92 1626         110221 return $message;
93             }
94              
95             __PACKAGE__->meta->make_immutable;
96             1;
97              
98             # ABSTRACT: Superclass for Moose internal exceptions
99              
100             __END__
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Moose::Exception - Superclass for Moose internal exceptions
109              
110             =head1 VERSION
111              
112             version 2.2203
113              
114             =head1 DESCRIPTION
115              
116             This class contains attributes which are common to all Moose internal
117             exception classes.
118              
119             =head1 WARNING WARNING WARNING
120              
121             If you're writing your own exception classes, you should instead prefer
122             the L<Throwable> role or the L<Throwable::Error> superclass - this is
123             effectively a cut-down internal fork of the latter, and not designed
124             for use in user code.
125              
126             Of course if you're writing metaclass traits, it would then make sense to
127             subclass the relevant Moose exceptions - but only then.
128              
129             =head1 METHODS
130              
131             =head2 $exception->message
132              
133             This attribute contains the exception message.
134              
135             Every subclass of L<Moose::Exception> is expected to override
136             C<_build_message> method in order to construct this value.
137              
138             =head2 $exception->trace
139              
140             This attribute contains the stack trace for the given exception. It returns a
141             L<Devel::StackTrace> object.
142              
143             =head2 $exception->as_string
144              
145             This method returns a stringified form of the exception, including a stack
146             trace. By default, this method skips Moose-internal stack frames until it sees
147             a caller outside of the Moose core. If the C<MOOSE_FULL_EXCEPTION> environment
148             variable is true, these frames are included.
149              
150             =head1 SEE ALSO
151              
152             =over 4
153              
154             =item * L<Moose::Manual::Exceptions>
155              
156             =back
157              
158             =head1 AUTHORS
159              
160             =over 4
161              
162             =item *
163              
164             Stevan Little <stevan@cpan.org>
165              
166             =item *
167              
168             Dave Rolsky <autarch@urth.org>
169              
170             =item *
171              
172             Jesse Luehrs <doy@cpan.org>
173              
174             =item *
175              
176             Shawn M Moore <sartak@cpan.org>
177              
178             =item *
179              
180             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
181              
182             =item *
183              
184             Karen Etheridge <ether@cpan.org>
185              
186             =item *
187              
188             Florian Ragwitz <rafl@debian.org>
189              
190             =item *
191              
192             Hans Dieter Pearcey <hdp@cpan.org>
193              
194             =item *
195              
196             Chris Prather <chris@prather.org>
197              
198             =item *
199              
200             Matt S Trout <mstrout@cpan.org>
201              
202             =back
203              
204             =head1 COPYRIGHT AND LICENSE
205              
206             This software is copyright (c) 2006 by Infinity Interactive, Inc.
207              
208             This is free software; you can redistribute it and/or modify it under
209             the same terms as the Perl 5 programming language system itself.
210              
211             =cut