File Coverage

lib/Workflow/Exception.pm
Criterion Covered Total %
statement 42 42 100.0
branch 2 2 100.0
condition n/a
subroutine 13 13 100.0
pod 6 6 100.0
total 63 63 100.0


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