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.2205';
3              
4 185     185   175846 use Moose;
  185         553  
  185         1301  
5 185     185   104973 use Devel::StackTrace 2.03;
  185         655672  
  185         18904  
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 185         1235 fallback => 1,
29 185     185   1574 );
  185         533  
30              
31             sub _build_trace {
32 3684     3684   6154 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 3684         5581 my $skip = 0;
38 3684         31503 while (my @c = caller(++$skip)) {
39 11070 100 100     110264 last if ($c[3] =~ /^(.*)::new$/ || $c[3] =~ /^\S+ (.*)::new \(defined at /)
      66        
40             && $self->isa($1);
41             }
42 3684         8778 $skip++;
43              
44 3684         98902 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 3701     3701 0 6327 my $self = shift;
58 3701         107582 $self->trace;
59             }
60              
61             sub as_string {
62 1626     1626 1 571234 my $self = shift;
63              
64 1626 100       6084 if ( $ENV{MOOSE_FULL_EXCEPTION} ) {
65 5         158 return $self->trace->as_string;
66             }
67              
68 1621         3507 my @frames;
69             my $last_frame;
70 1621         2808 my $in_moose = 1;
71 1621         53608 for my $frame ( $self->trace->frames ) {
72 19083 100 100     976094 if ( $in_moose && $frame->package =~ /^(?:Moose|Class::MOP)(?::|$)/ )
    100          
73             {
74 2169         15504 $last_frame = $frame;
75 2169         3704 next;
76             }
77             elsif ($last_frame) {
78 454         3591 push @frames, $last_frame;
79 454         947 undef $last_frame;
80             }
81              
82 16914         30017 $in_moose = 0;
83 16914         22995 push @frames, $frame;
84             }
85              
86             # This would be a somewhat pathological case, but who knows
87 1621 50       5073 return $self->trace->as_string unless @frames;
88              
89 1621         8755 my $message = ( shift @frames )->as_string( 1, {} ) . "\n";
90 1621         46637 $message .= join q{}, map { $_->as_string( 0, {} ) . "\n" } @frames;
  15747         774337  
91              
92 1621         128610 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.2205
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