-
Notifications
You must be signed in to change notification settings - Fork 11
/
Exception.pm
243 lines (165 loc) · 6.05 KB
/
Exception.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package Workflow::Exception;
use warnings;
use strict;
use v5.14.0;
# Declare some of our exceptions...
use Exception::Class (
'Workflow::Exception::Configuration' => {
isa => 'Workflow::Exception',
description => 'Configuration errors',
},
'Workflow::Exception::Persist' => {
isa => 'Workflow::Exception',
description => 'Persistence errors',
},
'Workflow::Exception::Validation' => {
isa => 'Workflow::Exception',
description => 'Validation errors',
fields => 'invalid_fields',
},
);
use Log::Any;
my %TYPE_CLASSES = (
configuration_error => 'Workflow::Exception::Configuration',
persist_error => 'Workflow::Exception::Persist',
validation_error => 'Workflow::Exception::Validation',
workflow_error => 'Workflow::Exception',
);
my %TYPE_LOGGING = (
configuration_error => 'error',
persist_error => 'error',
validation_error => 'info',
workflow_error => 'error',
);
$Workflow::Exception::VERSION = '1.57';
@Workflow::Exception::ISA = qw( Exporter Exception::Class::Base );
@Workflow::Exception::EXPORT_OK = keys %TYPE_CLASSES;
# Exported shortcuts
sub _mythrow {
my ( $type, @items ) = @_;
my ( $msg, %params ) = _massage(@items);
my $caller = caller;
my $log = Log::Any->get_logger( category => $caller ); # log as if part of the package of the caller
my ( $pkg, $line ) = (caller)[ 0, 2 ];
my ( $prev_pkg, $prev_line ) = ( caller 1 )[ 0, 2 ];
# Do not log condition errors
my $method = $TYPE_LOGGING{$type};
$log->$method(
"$type exception thrown from [$pkg: $line; before: ",
"$prev_pkg: $prev_line]: $msg"
);
goto &Exception::Class::Base::throw(
$TYPE_CLASSES{$type},
message => $msg,
%params
);
}
# Use 'goto' here to maintain the stack trace
sub configuration_error {
unshift @_, 'configuration_error';
goto &_mythrow;
}
sub persist_error {
unshift @_, 'persist_error';
goto &_mythrow;
}
sub validation_error {
unshift @_, 'validation_error';
goto &_mythrow;
}
sub workflow_error {
unshift @_, 'workflow_error';
goto &_mythrow;
}
# Override 'throw' so we can massage the message and parameters into
# the right format for E::C
sub throw {
my ( $class, @items ) = @_;
my ( $msg, %params ) = _massage(@items);
goto &Exception::Class::Base::throw( $class, message => $msg, %params );
}
sub _massage {
my @items = @_;
my %params = ( ref $items[-1] eq 'HASH' ) ? %{ pop @items } : ();
my $msg = join '', @items;
$msg =~ s/\\n/ /g; # don't log newlines as per Log4perl recommendations
return ( $msg, %params );
}
1;
__END__
=pod
=head1 NAME
Workflow::Exception - Base class for workflow exceptions
=head1 VERSION
This documentation describes version 1.57 of this package
=head1 SYNOPSIS
# Standard usage
use Workflow::Exception qw( workflow_error );
my $user = $wf->context->param( 'current_user' );
unless ( $user->check_password( $entered_password ) ) {
workflow_error "User exists but password check failed";
}
# Pass a list of strings to form the message
unless ( $user->check_password( $entered_password ) ) {
workflow_error 'Bad login: ', $object->login_attempted;
}
# Using other exported shortcuts
use Workflow::Exception qw( configuration_error );
configuration_error "Field 'foo' must be a set to 'bar'";
use Workflow::Exception qw( validation_error );
validation_error "Validation for field 'foo' failed: $error";
=head1 DESCRIPTION
First, you should probably look at
L<Exception::Class|Exception::Class> for more usage examples, why we
use exceptions, what they are intended for, etc.
This is the base class for all workflow exceptions. It declares a
handful of exceptions and provides shortcuts to make raising an
exception easier and more readable.
=head1 METHODS
=head3 throw( @msg, [ \%params ])
This overrides B<throw()> from L<Exception::Class|Exception::Class> to
add a little syntactic sugar. Instead of:
$exception_class->throw( message => 'This is my very long error message that I would like to pass',
param1 => 'Param1 value',
param2 => 'Param2 value' );
You can use:
$exception_class->throw( 'This is my very long error message ',
'that I would like to pass',
{ param1 => 'Param1 value',
param2 => 'Param2 value' } );
And everything will work the same. Combined with the L<SHORTCUTS> this
makes for very readable code:
workflow_error "Something went horribly, terribly, dreadfully, "
"frightfully wrong: $@",
{ foo => 'bar' };
=head3 configuration_error
This method transforms the error to a configuration error.
This exception is thrown via </mythrow> when configuration of a workflow is unsuccessful.
=head3 persist_error
This method transforms the error to a persistance error.
This exception is thrown via </mythrow> when the save of a workflow is unsuccessful.
=head3 validation_error
This method transforms the error to a validation error.
This exception is thrown via </mythrow> when input data or similar of a workflow is unsuccessful.
=head3 workflow_error
This method transforms the error to a workflow error.
This exception is thrown via </mythrow> when input data or similar of a workflow is unsuccessful.
=head1 SHORTCUTS
=over
=item * B<Workflow::Exception> - import using C<workflow_error>
=item * B<Workflow::Exception::Configuration> - import using C<configuration_error>
=item * B<Workflow::Exception::Persist> - import using C<persist_error>
=item * B<Workflow::Exception::Validation> - import using C<validation_error>
=back
=head1 SEE ALSO
=over
=item * L<Exception::Class|Exception::Class>
=back
=head1 COPYRIGHT
Copyright (c) 2003-2021 Chris Winters. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Please see the F<LICENSE>
=head1 AUTHORS
Please see L<Workflow>
=cut