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