blib/lib/Error/Helper.pm

filter: Statementstmt 17.7% 8 / 45 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 22.2% 2 / 9 Podpod 100% 7 / 7 totaltotal 21.7% 17 / 78 SCAR 36.5
CC
7
Cov
14%
errorFlag
30.0
errorblank
6.0
warnString
6.0
CRAP 38.6
↑ Summary
1 package Error::Helper;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Error::Helper - Provides some easy error related methods.
9  
10 =head1 VERSION
11  
12 Version 1.0.0
13  
14 =cut
15  
16 2 our $VERSION = '1.0.0';
17  
18  
19 =head1 SYNOPSIS
20  
21 Below is a example module using this.
22  
23 package Foo;
24
25 use warnings;
26 use strict;
27 use base 'Error::Helper';
28
29 sub new{
30 my $arg=$_[1];
31  
32 my $self = {
33 perror=>undef,
34 error=>undef,
35 errorString=>"",
36 errorExtra=>{
37 flags=>{
38 1=>'UndefArg',
39 2=>'test',
40 }
41 }.
42 };
43 bless $self;
44  
45 #error if $arg is set to "test"
46 if( $arg eq "test" ){
47 $self->{perror}=1;
48 $self->{error}=2;
49 $self->{errorString}='A value of "test" has been set';
50 $self->warn;
51 return $self;
52 }
53  
54 return undef;
55 }
56  
57 sub foo{
58 my $self=$_[0];
59 my $a=$_[1];
60  
61 if( ! $self->errorblank ){
62 return undef;
63 }
64  
65 if( !defined( $a ) ){
66 $self->{error}=1;
67 $self->{errorString}='No value specified';
68 $self->warn;
69 return undef;
70 }
71  
72 return 1;
73 }
74  
75 Below is a example script.
76  
77 use Foo;
78
79 my $foo=Foo->new( $ARGV[0] );
80 if( $foo->error ){
81 warn('error:'.$foo->error.': '.$foo->errorString);
82 exit $foo->error;
83 }
84
85 $foo->foo($ARGV[1]);
86 if( $foo->error ){
87 warn('error:'.$foo->error.': '.$foo->errorString);
88 exit $foo->error;
89 }
90  
91 There are three required variables in the blessed hash.
92  
93 $self->{error}
94  
95 This contains the current error code.
96  
97 $self->{errorString}
98  
99 This contains a description of the current error.
100  
101 $self->{perror}
102  
103 This is set to true is a permanent error is present. If note,
104 it needs set to false.
105  
106 $self->{errorExtra}
107  
108 This is a hash reserved for any additional Error::Helper stuff
109 that may be added at a latter date.
110  
111 $self->{errorExtra}{flags}
112  
113 This hash contains error integer to flag mapping. The keys are
114 the error integer and the value is the flag.
115  
116 For any unmatched error integers, 'other' is returned.
117  
118 =head1 METHODS
119  
120 =head2 error
121  
122 Returns the current error code and true if there is an error.
123  
124 If there is no error, undef is returned.
125  
126 if($self->error){
127 warn('error: '.$foo->error.":".$foo->errorString);
128 }
129  
130 =cut
131  
132 sub error{
133 0 ▶ return $_[0]->{error};
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
134 }
135  
136 =head2 errorblank
137  
138 This blanks the error storage and is only meant for internal usage.
139  
140 It does the following.
141  
142 If $self->{perror} is set, it will not be able to blank any current
143 errors.
144  
145 $self->{error}=undef;
146 $self->{errorString}="";
147  
148 =cut
149  
150 sub errorblank{
151 0 ▶ my $self=$_[0];
Subroutine errorblank: not called CC=2 SCAR=17.9
Pod documented
152  
153 0 ▶ if ($self->{perror}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"perror"}) 0 0 0
154 0 my ($package, $filename, $line)=caller;
155
156 #get the calling sub
157 0 my @called=caller( 1 );
158 0 my $subroutine=$called[3];
159 0 $subroutine=~s/.*\:\://g;
160
161 0 $package =~ s/\:\:/\-/g;
162  
163 0 print STDERR $package.' '.$subroutine.': Unable to blank, as a permanent error is set. '.
164 'error="'.$self->error.'" errorString="'.$self->errorString.'"';
165  
166 0 return undef;
167 }
168  
169 0 $self->{error}=undef;
170 0 $self->{errorString}="";
171  
172 0 return 1;
173 };
174  
175 =head2 errorFlag
176  
177 This returns the error flag for the current error.
178  
179 If none is set, undef is returned.
180  
181 This may be used in a similar manner as the error method.
182  
183 if ( $self->errorFlag ){
184 warn('error: '.$self->error.":".$self->errorFlag.":".$self->errorString);
185 }
186  
187 =cut
188  
189 sub errorFlag{
190 0 ▶ if ( ! $_[0]->{error} ){
Subroutine errorFlag: not called CC=5 SCAR=34.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]{"error"}) 0 0 0
191 0 return undef;
192 }
193  
194 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"errorExtra"} and defined $_[0]{"errorExtra"}{"flags"} and defined $_[0]{"errorExtra"}{"flags"}{$_[1]{"error"}}) 0 0 0
Condition0%0/6
defined $_[0]{"errorExtra"} and defined $_[0]{"errorExtra"}{"flags"} and defined $_[0]{"errorExtra"}{"flags"}{$_[1]{"error"}}
!ll&&!rl&&r
000
defined $_[0]{"errorExtra"} and defined $_[0]{"errorExtra"}{"flags"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_[0]{"errorExtra"} and defined $_[0]{"errorExtra"}{"flags"} and defined $_[0]{"errorExtra"}{"flags"}{$_[1]{"error"}}
defined $_[0]{"errorExtra"} defined $_[0]{"errorExtra"}{"flags"} defined $_[0]{"errorExtra"}{"flags"}{$_[1]{"error"}}
Truth table0%0/4
A and B and C
A= defined $_[0]{"errorExtra"}B= defined $_[0]{"errorExtra"}{"flags"}C= defined $_[0]{"errorExtra"}{"flags"}{$_[1]{"error"}}
ABCresult
0XX0
10X0
1100
1111
195 ( ! defined( $_[0]->{errorExtra} ) ) ||
196 ( ! defined( $_[0]->{errorExtra}{flags} ) ) ||
197 ( ! defined( $_[0]->{errorExtra}{flags}{ $_[1]->{error} } ) )
198 ){
199 0 return 'other';
200 }
201  
202 0 return $_[0]->{errorExtra}{flags}{ $_[1]->{error} };
203 }
204  
205 =head2 errorString
206  
207 Returns the error string if there is one. If there is not,
208 it will return ''.
209  
210 if($self->error){
211 warn('error: '.$self->error.":".$self->errorString);
212 }
213  
214 =cut
215  
216 sub errorString{
217 0 ▶ return $_[0]->{errorString};
Subroutine errorString: not called CC=1 SCAR=6.9
Pod documented
218 }
219  
220 =head2 perror
221  
222 This returns a Perl boolean for if there is a permanent
223 error or not.
224  
225 if($self->perror){
226 warn('A permanent error is set');
227 }
228  
229 =cut
230  
231 sub perror{
232 0 ▶ return $_[0]->{perror};
Subroutine perror: not called CC=1 SCAR=6.9
Pod documented
233 }
234  
235 =head2 warn
236  
237 Throws a warn like error message based
238  
239 $self->warn;
240  
241 =cut
242  
243 sub warn{
244 0 ▶ my $self=$_[0];
Subroutine warn: not called CC=1 SCAR=6.9
Pod documented
245
246 0 my ($package, $filename, $line)=caller;
247  
248 #get the calling sub
249 0 my @called=caller( 1 );
250 0 my $subroutine=$called[3];
251 0 $subroutine=~s/.*\:\://g;
252  
253 0 $package =~ s/\:\:/\-/g;
254  
255 0 print STDERR $package.' '.$subroutine.':'.$self->error.
256 ': '.$self->errorString.' at '.$filename.' line '.$line."\n";
257 }
258  
259 =head2 warnString
260  
261 Throws a warn like error in the same for mate as warn, but with a freeform message.
262  
263 $self->warnString('some error');
264  
265 =cut
266  
267 sub warnString{
268 0 ▶ my $self=$_[0];
Subroutine warnString: not called CC=2 SCAR=17.9
Pod documented
269 0 my $string=$_[1];
270
271 0 ▶ if(!defined($string)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 0 0
272 0 $string='undef';
273 }
274
275 0 my ($package, $filename, $line)=caller;
276  
277 #get the calling sub
278 0 my @called=caller( 1 );
279 0 my $subroutine=$called[3];
280 0 $subroutine=~s/.*\:\://g;
281  
282 0 $package =~ s/\:\:/\-/g;
283  
284 0 print STDERR $package.' '.$subroutine.': '.$string.' in '.$filename.' at line '.$line."\n";
285 }
286  
287 =head1 ERROR FLAGS
288  
289 Error flags are meant to be short non-spaced strings that are easier to remember than a specific error integer.
290  
291 'other' is the generic error flag for when one is not defined.
292  
293 An error flag should never evaluate to false if an error is present.
294  
295 =head1 AUTHOR
296  
297 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
298  
299 =head1 BUGS
300  
301 Please report any bugs or feature requests to C<bug-error-helper at rt.cpan.org>, or through
302 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Error-Helper>. I will be notified, and then you'll
303 automatically be notified of progress on your bug as I make changes.
304  
305  
306  
307  
308 =head1 SUPPORT
309  
310 You can find documentation for this module with the perldoc command.
311  
312 perldoc Error::Helper
313  
314  
315 You can also look for information at:
316  
317 =over 4
318  
319 =item * RT: CPAN's request tracker
320  
321 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Error-Helper>
322  
323 =item * AnnoCPAN: Annotated CPAN documentation
324  
325 L<http://annocpan.org/dist/Error-Helper>
326  
327 =item * CPAN Ratings
328  
329 L<http://cpanratings.perl.org/d/Error-Helper>
330  
331 =item * Search CPAN
332  
333 L<http://search.cpan.org/dist/Error-Helper/>
334  
335 =back
336  
337 =head1 LICENSE AND COPYRIGHT
338  
339 Copyright 2012 Zane C. Bowers-Hadley.
340  
341 This program is free software; you can redistribute it and/or modify it
342 under the terms of either: the GNU General Public License as published
343 by the Free Software Foundation; or the Artistic License.
344  
345 See http://dev.perl.org/licenses/ for more information.
346  
347  
348 =cut
349  
350 2 1; # End of Error::Helper
↑ Summary