line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::Exception::Base; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Created on: 2012-07-11 10:25:25 |
4
|
|
|
|
|
|
|
# Create by: Ivan Wills |
5
|
|
|
|
|
|
|
# $Id$ |
6
|
|
|
|
|
|
|
# $Revision$, $HeadURL$, $Date$ |
7
|
|
|
|
|
|
|
# $Revision$, $Source$, $Date$ |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
15684
|
use Moose; |
|
2
|
|
|
|
|
349093
|
|
|
2
|
|
|
|
|
15
|
|
10
|
2
|
|
|
2
|
|
10529
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
55
|
|
11
|
2
|
|
|
2
|
|
1007
|
use version; |
|
2
|
|
|
|
|
2668
|
|
|
2
|
|
|
|
|
10
|
|
12
|
2
|
|
|
2
|
|
122
|
use Carp qw/longmess/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
121
|
|
13
|
2
|
|
|
2
|
|
730
|
use MooseX::Exception::Base::Stringify; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
69
|
|
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
11
|
use overload '""' => 'verbose'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
16
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = version->new('0.0.5'); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has error => ( |
20
|
|
|
|
|
|
|
is => 'rw', |
21
|
|
|
|
|
|
|
isa => 'Str', |
22
|
|
|
|
|
|
|
traits => [qw{MooseX::Exception::Stringify}], |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
has _stack => ( |
25
|
|
|
|
|
|
|
is => 'rw', |
26
|
|
|
|
|
|
|
isa => 'Str', |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
has _verbose => ( |
29
|
|
|
|
|
|
|
is => 'rw', |
30
|
|
|
|
|
|
|
isa => 'Int', |
31
|
|
|
|
|
|
|
default => 0, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub throw { |
35
|
6
|
|
|
6
|
1
|
17471
|
my $class = shift; |
36
|
6
|
100
|
|
|
|
26
|
my %args = @_ == 1 ? %{$_[0]} : @_; |
|
1
|
|
|
|
|
3
|
|
37
|
|
|
|
|
|
|
|
38
|
6
|
|
|
|
|
808
|
$args{_stack} = longmess(''); |
39
|
|
|
|
|
|
|
|
40
|
6
|
|
|
|
|
101
|
die $class->new(%args); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _stringify_attributes { |
44
|
26
|
|
|
26
|
|
20
|
my ($self) = @_; |
45
|
26
|
|
|
|
|
64
|
my $meta = $self->meta; |
46
|
|
|
|
|
|
|
|
47
|
26
|
|
|
|
|
288
|
my @parent_nodes; |
48
|
26
|
|
|
|
|
53
|
my @supers = $meta->superclasses; |
49
|
26
|
|
|
|
|
764
|
for my $super (@supers) { |
50
|
26
|
100
|
|
|
|
127
|
if ( $super->can('_stringify_attributes') ) { |
51
|
10
|
|
|
|
|
19
|
push @parent_nodes, $super->_stringify_attributes; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
24
|
|
|
|
|
1386
|
return @parent_nodes, map { |
56
|
2
|
|
|
|
|
269
|
$meta->get_attribute($_) |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
sort { |
59
|
60
|
|
|
|
|
3697
|
$meta->get_attribute($a)->insertion_order <=> $meta->get_attribute($b)->insertion_order |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
grep { |
62
|
26
|
|
|
|
|
127
|
$meta->get_attribute($_)->does('MooseX::Exception::Stringify') |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
$meta->get_attribute_list; |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub verbose { |
68
|
16
|
|
|
16
|
1
|
9845
|
my ($self, $verbose) = @_; |
69
|
16
|
|
|
|
|
18
|
my @errors; |
70
|
16
|
|
|
|
|
37
|
my @attributes = $self->_stringify_attributes; |
71
|
|
|
|
|
|
|
|
72
|
16
|
|
|
|
|
233
|
for my $attribute (@attributes) { |
73
|
24
|
|
|
|
|
42
|
my $name = $attribute->name; |
74
|
24
|
100
|
|
|
|
539
|
next if !defined $self->$name; |
75
|
|
|
|
|
|
|
|
76
|
22
|
|
|
|
|
485
|
local $_ = $self->$name; |
77
|
22
|
100
|
|
|
|
718
|
push @errors, |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
78
|
|
|
|
|
|
|
( $attribute->has_stringify_pre ? $attribute->stringify_pre : '' ) |
79
|
|
|
|
|
|
|
. ( $attribute->has_stringify ? $attribute->stringify->($_) : $_ ) |
80
|
|
|
|
|
|
|
. ( $attribute->has_stringify_post ? $attribute->stringify_post : '' ); |
81
|
|
|
|
|
|
|
} |
82
|
16
|
|
|
|
|
37
|
my $error = join "\n", @errors; |
83
|
|
|
|
|
|
|
|
84
|
16
|
100
|
|
|
|
327
|
$verbose = defined $verbose ? $verbose : $self->_verbose; |
85
|
16
|
100
|
|
|
|
126
|
my $stack |
|
|
100
|
|
|
|
|
|
86
|
|
|
|
|
|
|
= $verbose == 0 ? '' |
87
|
|
|
|
|
|
|
: $verbose == 1 ? (split /\n/, $self->_stack)[0] |
88
|
|
|
|
|
|
|
: $self->_stack; |
89
|
|
|
|
|
|
|
|
90
|
16
|
|
|
|
|
72
|
return $error . $stack; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 NAME |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
MooseX::Exception::Base - Base class for exceptions |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 VERSION |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
This documentation refers to MooseX::Exception::Base version 0.0.5. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
package MyException; |
108
|
|
|
|
|
|
|
use Moose; |
109
|
|
|
|
|
|
|
extends 'MooseX::Exception::Base'; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
has code => ( is => 'rw', isa => 'Num' ); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
package MyOtherException; |
114
|
|
|
|
|
|
|
use Moose; |
115
|
|
|
|
|
|
|
extends 'MooseX::Exception::Base'; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
has message => ( |
118
|
|
|
|
|
|
|
is => 'rw', |
119
|
|
|
|
|
|
|
isa => 'Str', |
120
|
|
|
|
|
|
|
traits => [qw{MooseX::Exception::Stringify}], |
121
|
|
|
|
|
|
|
stringify_pre => 'prefix string ', |
122
|
|
|
|
|
|
|
stringify_post => ' postfix string', |
123
|
|
|
|
|
|
|
# a subroutine that returns a stringified value eg custom DateTime formatting |
124
|
|
|
|
|
|
|
stringify => sub {return $_}, |
125
|
|
|
|
|
|
|
); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# ... else where |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
use MyException; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub mysub { |
132
|
|
|
|
|
|
|
MyException->throw( error => 'My error', code => 666 ); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
eval { mysub() }; |
136
|
|
|
|
|
|
|
if ($@) { |
137
|
|
|
|
|
|
|
warn "ERROR : $e\n"; |
138
|
|
|
|
|
|
|
# or |
139
|
|
|
|
|
|
|
warn $e->verbose; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub myother { |
143
|
|
|
|
|
|
|
MyOtherException->throw( |
144
|
|
|
|
|
|
|
message => "Custom error message", |
145
|
|
|
|
|
|
|
); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
eval { myother() }; |
149
|
|
|
|
|
|
|
if ($@) { |
150
|
|
|
|
|
|
|
warn "ERROR : $e\n"; |
151
|
|
|
|
|
|
|
# or |
152
|
|
|
|
|
|
|
warn $e->verbose; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 DESCRIPTION |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
This module provides basic helpers to make Moose exception objects, is is |
158
|
|
|
|
|
|
|
somewhat similar to L<Exception::Class> in usage. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=over 4 |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item C<throw (%args)> |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Throw an exception with object with the parameters from C<%args> |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item C<verbose ([$verbosity])> |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Stringifys the exception object, if C<$verbosidy> is not passed the classes |
171
|
|
|
|
|
|
|
attribute _verbose is used. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=back |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 DIAGNOSTICS |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 CONFIGURATION AND ENVIRONMENT |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 INCOMPATIBILITIES |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 BUGS AND LIMITATIONS |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
There are no known bugs in this module. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Please report problems to Ivan Wills (ivan.wills@gmail.com). |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Patches are welcome. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 ALSO SEE |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
L<Throwable> probably should be use rather than this module for new projects |
194
|
|
|
|
|
|
|
as it's now best practice. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
L<Moose>, L<Exception::Class> |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 AUTHOR |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Ivan Wills - (ivan.wills@gmail.com) |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Adam Herzog - adam@adamherzog.com |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Copyright (c) 2012 Ivan Wills (14 Mullion Close Hornsby Heights NSW Australia 2077). |
209
|
|
|
|
|
|
|
All rights reserved. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it under |
212
|
|
|
|
|
|
|
the same terms as Perl itself. See L<perlartistic>. This program is |
213
|
|
|
|
|
|
|
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
214
|
|
|
|
|
|
|
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
215
|
|
|
|
|
|
|
PARTICULAR PURPOSE. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=cut |