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.2206';
3              
4 185     185   173605 use Moose;
  185         486  
  185         1169  
5 185     185   104184 use Devel::StackTrace 2.03;
  185         651216  
  185         18733  
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         1150 fallback => 1,
29 185     185   1616 );
  185         605  
30              
31             sub _build_trace {
32 3684     3684   6038 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         5464 my $skip = 0;
38 3684         30042 while (my @c = caller(++$skip)) {
39 11070 100 100     104200 last if ($c[3] =~ /^(.*)::new$/ || $c[3] =~ /^\S+ (.*)::new \(defined at /)
      66        
40             && $self->isa($1);
41             }
42 3684         8751 $skip++;
43              
44 3684         94335 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 6058 my $self = shift;
58 3701         101997 $self->trace;
59             }
60              
61             sub as_string {
62 1626     1626 1 572879 my $self = shift;
63              
64 1626 100       5996 if ( $ENV{MOOSE_FULL_EXCEPTION} ) {
65 5         160 return $self->trace->as_string;
66             }
67              
68 1621         3363 my @frames;
69             my $last_frame;
70 1621         2960 my $in_moose = 1;
71 1621         53271 for my $frame ( $self->trace->frames ) {
72 19083 100 100     977159 if ( $in_moose && $frame->package =~ /^(?:Moose|Class::MOP)(?::|$)/ )
    100          
73             {
74 2169         15338 $last_frame = $frame;
75 2169         3629 next;
76             }
77             elsif ($last_frame) {
78 454         3674 push @frames, $last_frame;
79 454         928 undef $last_frame;
80             }
81              
82 16914         30285 $in_moose = 0;
83 16914         24386 push @frames, $frame;
84             }
85              
86             # This would be a somewhat pathological case, but who knows
87 1621 50       5250 return $self->trace->as_string unless @frames;
88              
89 1621         11238 my $message = ( shift @frames )->as_string( 1, {} ) . "\n";
90 1621         46534 $message .= join q{}, map { $_->as_string( 0, {} ) . "\n" } @frames;
  15747         772870  
91              
92 1621         128329 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.2206
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