Easy/Error.pm

filter: Statementstmt 29.6% 38 / 128 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 42.8% 9 / 21 Podpod 0.0% 0 / 2 totaltotal 19.3% 47 / 243 CC 36 SCAR 37.7
CC
36
Cov
19%
_exception
132.0
_assign
54.1
_const
42.0
CRAP 43.2
« Easy/Common.pm ↑ Summary Easy/Cursor.pm »
1 package BerkeleyDB::Easy::Error;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 ▶use BerkeleyDB::Easy::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 our (@ISA, @EXPORT, %EXPORT_TAGS, %Errors);
9
10 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 constant->import({ CODE => 0, DESC => 1, SKIP => 3 });
12 }
13
14 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
15 1 @ISA = qw(Exporter);
16 1 @EXPORT = ();
17 1 %EXPORT_TAGS = (
18 subs => [qw(_exception _throw _assign _const _lookup _caller _carp)],
19 );
20
21 # All our constants get BDB_ prefix. And they're dualvars, with neg
22 # values to avoid stepping on toes.
23 1 %Errors = (
24 BDB_DEFAULT => [-900, q(Default error) ],
25 BDB_UNKNOWN => [-404, q(Unknown error) ],
26 BDB_PLACE => [-666, q(Placeholder error) ],
27 BDB_HANDLE => [-902, q(Failed to create BerkeleyDB handle) ],
28 BDB_TYPE => [-901, q(Invalid BerkeleyDB database type) ],
29 BDB_FLAG => [-903, q(Invalid options flag) ],
30 BDB_PARAM => [-904, q(Invalid options parameter) ],
31 BDB_CONST => [-905, q(Invalid constant function) ],
32 );
33
34 # Create constants and make them available for export under the
35 # 'errors' tag.
36 1 for my $name (keys %Errors) {
37 8 my $code = $Errors{$name}->[CODE];
38 8 constant->import($name, Scalar::Util::dualvar($code, $name));
39 8 push @{$EXPORT_TAGS{errors}}, $name;
40 }
41
42 # Export everything.
43 1 push @EXPORT, map @{$EXPORT_TAGS{$_}}, keys %EXPORT_TAGS;
44 }
45
46 #
47 # Define the attributes for exception objects. _install called with a
48 # single argument as done here creates a simple named accessor
49 #
50 1 for (qw(code name time level desc detail package file line sub trace)) {
51 11 __PACKAGE__->_install($_);
52 }
53
54 #
55 # Stringify an exception object. Create default message if none is set.
56 #
57 0 ▶sub stringify {
Subroutine stringify: not called CC=3 SCAR=24.8
Pod undocumented
58 0 my $self = shift;
59
60 $self->{message} ||= join q(. ), grep $_,
61 $self->{desc},
62 0 ▶ $self->{detail};
Condition0%0/3
$self->{"message"} ||= join(". ", grep($_, $self->{"desc"}, $self->{"detail"}))
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"message"} ||= join(". ", grep($_, $self->{"desc"}, $self->{"detail"}))
$self->{"message"} join(". ", grep($_, $self->{"desc"}, $self->{"detail"}))
Truth table0%0/3
A ||= B
A= $self->{"message"}B= join(". ", grep($_, $self->{"desc"}, $self->{"detail"}))
ABresult
1X1
011
000
63
64 $self->{string} ||= sprintf q([%s] %s (%d): %s %s),
65 $self->{sub},
66 $self->{name},
67 $self->{code},
68 $self->{message},
69 0 ▶ $self->{trace};
Condition0%0/3
$self->{"string"} ||= sprintf("[%s] %s (%d): %s %s", $self->{"sub"}, $self->{"name"}, $self->{"code"}, $self->{"message"}, $self->{"trace"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"string"} ||= sprintf("[%s] %s (%d): %s %s", $self->{"sub"}, $self->{"name"}, $self->{"code"}, $self->{"message"}, $self->{"trace"})
$self->{"string"} sprintf("[%s] %s (%d): %s %s", $self->{"sub"}, $self->{"name"}, $self->{"code"}, $self->{"message"}, $self->{"trace"})
Truth table0%0/3
A ||= B
A= $self->{"string"}B= sprintf("[%s] %s (%d): %s %s", $self->{"sub"}, $self->{"name"}, $self->{"code"}, $self->{"message"}, $self->{"trace"})
ABresult
1X1
011
000
70 }
71
72 0 ▶sub numberify { shift->{code} }
Subroutine numberify: not called CC=1 SCAR=6.9
Pod undocumented
73
74 1 ▶use overload fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
75 q("") => q(stringify),
76 1 q(0+) => q(numberify);
77
78 #
79 # Throw an exception. First, get it's severity level and ignore it if
80 # appropriate. Otherwise call _exception to build the error object and
81 # _log to log it and warn/die as necessary.
82 #
83 0 ▶sub _throw {
Subroutine _throw: not called CC=2 SCAR=17.9
84 0 my ($self, $error, $extra, $flag) = @_;
85
86 0 DEBUG and do {
87 my $code = int($error) || q(?);
88 $self->_debug(qq(Throwing "$error" ($code)));
89 };
90
91 0 my $level = $self->_assign($error);
92 0 ▶ if ($level == BDB_IGNORE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == "BDB_IGNORE") 0 0 0
93 0 TRACE and $self->_trace(qq(Ignoring exception: $error));
94 0 return;
95 }
96
97 0 my $exc = $self->_exception($error, $extra, $flag);
98 0 $exc->{level} = $level;
99 0 $self->_log($level, $exc);
100
101 0 $exc;
102 }
103
104 #
105 # Build an exception.
106 # (Internal method, used by _throw)
107 #
108 0 ▶sub _exception {
Subroutine _exception: not called CC=11 SCAR=48.8
109 0 my ($self, $error, $extra, $flag) = @_;
110
111 0 ▶ our $HiRes ||= !!$self->_try(sub { require Time::HiRes });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
our $HiRes ||= !!$self->_try(sub { require Time::HiRes; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
our $HiRes ||= !!$self->_try(sub { require Time::HiRes; } )
our $HiRes !!$self->_try(sub { require Time::HiRes; } )
Truth table0%0/3
A ||= B
A= our $HiResB= !!$self->_try(sub { require Time::HiRes; } )
ABresult
1X1
011
000
112 0 ▶ my %exc = (
Branch: 1 branch
BranchTrueFalseTotal
$BerkeleyDB::Easy::Error::HiRes ? : 0 0 0
Condition0%0/2
int $error || -404
l!l
00
MC/DC0%0/1
int $error || -404
int $error
Truth table0%0/2
A
A= int $error
Aresult
11
00
113 time => ($HiRes ? Time::HiRes::time() : time),
114 code => (int $error || int BDB_UNKNOWN),
115 );
116
117 # Populate package, file, line and sub attributes.
118 # If VERBOSE, get a full stack trace.
119 0 my $caller = $self->_caller(SKIP);
120 0 $exc{$_} = $caller->{$_} for qw(package file line sub);
121 0 $exc{trace} = BDB_VERBOSE
122 ? $self->_carp
123 : qq(at $exc{file} line $exc{line}.);
124
125 0 my @detail = $extra;
126
127 # TODO: a lot of this needs to be reworked. Misbehaving parts
128 # commented out.
129
130 # Gnarly logic here to determine where the error came from
131 # and consolidate diagnostic messages that were squirreled away
132 # into a nice object. From perlvar:
133 #
134 # $! = $OS_ERROR = $ERRNO : current value of the C errno integer.
135 # $^E = $EXTENDED_OS_ERROR : Error information specific to the current
136 # operating system. At the moment, this differs from $! under only
137 # VMS, OS/2, and Win32 (and for MacPerl). On all other platforms,
138 # $^E is always just the same as $! .
139
140 # DB_ prefix means error is from BerkeleyDB (the C library).
141 # Parse the exception into name and desc.
142 # If $! or $^E are also set, put them in the 'detail' field.
143 0 ▶ if ($error =~ /^DB_/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error =~ /^DB_/) { } 0 0 0
elsif ($error =~ /^BDB_/) { } 0 0 0
144 0 @exc{qw(name desc)} = $error =~ /^(DB_\w+):\s*(.+?)\.?$/;
145 0 ▶ push @detail, $!, ($^E ne $! and $^E) unless $flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag 0 0 0
Condition0%0/3
$^E ne $! && $^E
!ll&&!rl&&r
000
MC/DC0%0/2
$^E ne $! && $^E
$^E ne $! $^E
Truth table0%0/3
A && B
A= $^E ne $!B= $^E
ABresult
0X0
100
111
146 }
147
148 # Perl/OS error. Look up name from errno. Put $^E into 'detail'.
149 # If $flag is set, we never localized $! (due to optimization setting)
150 # so its value could be stale. In that case, skip this check.
151 # elsif ($! and not $flag) {
152 # @exc{qw(name desc)} = ($self->_lookup($!), $!);
153 # push @detail, ($^E ne $! and $^E);
154 # }
155
156 # Extended OS error. Usually won't appear without $!, but handle the
157 # possibility just in case. If $flag is set, we never localized $^E.
158 # elsif ($^E and not $flag) {
159 # @exc{qw(name desc)} = ($self->_lookup($^E), $^E);
160 # }
161
162 # BDB_ prefix means error was generated internally.
163 elsif ($error =~ /^BDB_/) {
164 0 @exc{qw(name desc)} = ($error, $Errors{$error}->[DESC]);
165 }
166
167 # Fallback. Not sure where error originated.
168 else {
169 0 @exc{qw(name desc)} = ($self->_lookup($error), $error);
170 }
171
172 # BerkeleyDB.pm error. Should only happen when there's a BerkeleyDB
173 # (C library) error during initialization. In that case, the BDB.pm
174 # error global will usually contain additional info.
175 0 ▶ if ($BerkeleyDB::Error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BerkeleyDB::Error) 0 0 0
176 0 my $match = qr/(?::\s*)?([^:]+?)\.?$/;
177 0 my ($err ) = $BerkeleyDB::Error =~ $match;
178 0 my ($desc) = $exc{desc} =~ $match;
179 0 ▶ push @detail, $err if $err ne $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $err ne $desc 0 0 0
180 }
181
182 # @detail may have accumulated multiple messages. Join them into one str.
183 0 $exc{detail} = join q(. ), map ucfirst, grep $_, @detail;
184
185 0 bless \%exc, $self->_Error;
186 }
187
188 #
189 # Look up or set the severity level of an error. Sets the level when the
190 # second argument ($level) is provided. This is done in the constructor
191 # if the user opts to assign non-default severity levels to one or more
192 # errors when a handle is created.
193 #
194 0 ▶sub _assign {
Subroutine _assign: not called CC=8 SCAR=39.9
195 0 my ($self, $error, $level) = @_;
196 0 ▶ return BDB_ERROR unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
197
198 # Look up error code from string
199 0 ▶ $error = $self->_const($error) if not int $error;
Branch: 1 branch
BranchTrueFalseTotal
unless int $error 0 0 0
200 0 my $code = int $error;
201
202 # The BerkeleyDB.pm handle object is inside-out since it's an XS library.
203 # Our handle is the same object reblessed into our class, so we can't
204 # store any attributes on it. Instead, look up the address and use it as
205 # the key for a class-global %Config hash, where we store instance
206 # settings.
207
208 0 my $handle = $self->_handle->[0];
209 0 ▶ our $Config ||= {};
Condition0%0/2
our $Config ||= {}
l!l
00
MC/DC0%0/1
our $Config ||= {}
our $Config
Truth table0%0/2
A
A= our $Config
Aresult
11
00
210
211 # Set severity level if we got $level
212 0 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 0 0 0
213 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
214 0 ▶ defined ${_Common . q(::Levels)}{$level}
Branch: 1 branch
BranchTrueFalseTotal
defined do { "BerkeleyDB::Easy::Common::Levels" }->{$level} or $self->_throw("BDB_FLAG", qq[Invalid error level "$level"]) 0 0 0
215 or $self->_throw(BDB_FLAG, qq(Invalid error level "$level"));
216 0 $Config->{$handle}{$code} = $level;
217 }
218
219 # Return user-supplied severity level or the default.
220 $Config->{$handle}{$code}
221 0 ▶ or $Config->{$handle}{int BDB_DEFAULT}
Branch: 1 branch
BranchTrueFalseTotal
$BerkeleyDB::Easy::Error::Config->{$handle}{$code} or $BerkeleyDB::Easy::Error::Config->{$handle}{-900} or "BDB_ERROR" 0 0 0
Condition0%0/3
$BerkeleyDB::Easy::Error::Config->{$handle}{$code} or $BerkeleyDB::Easy::Error::Config->{$handle}{-900}
l!l&&r!l&&!r
000
MC/DC0%0/2
$BerkeleyDB::Easy::Error::Config->{$handle}{$code} or $BerkeleyDB::Easy::Error::Config->{$handle}{-900}
$BerkeleyDB::Easy::Error::Config->{$handle}{$code} $BerkeleyDB::Easy::Error::Config->{$handle}{-900}
Truth table0%0/3
A or B
A= $BerkeleyDB::Easy::Error::Config->{$handle}{$code}B= $BerkeleyDB::Easy::Error::Config->{$handle}{-900}
ABresult
1X1
011
000
222 or BDB_ERROR;
223 }
224
225 #
226 # Resolve a system error name to its errno integer code.
227 # (Complement to _lookup. Internal method, used by _assign)
228 #
229 # Convenience function for option parsing, for when the user
230 # gives us a string erorr name instead of an int or dualvar.
231 #
232 0 ▶sub _const {
Subroutine _const: not called CC=6 SCAR=37.4
233 0 my ($self, $name) = @_;
234
235 0 DEBUG and $self->_debug(qq(Resolving constant: $name));
236
237 0 my $caller = $self->_caller(SKIP)->{package};
238 0 my $fullname = qq(&$caller\::$name);
239
240 # Resolve the name to a coderef. Look in our caller, this module,
241 # BerkeleyDB, and Errno, in that order.
242 my $func = $caller->can($name)
243 || $self->can($name)
244 0 || do { BerkeleyDB->can($name) }
245 0 ▶ || do { require Errno; Errno->can($name) }
Branch: 1 branch
BranchTrueFalseTotal
my $func = $caller->can($name) || $self->can($name) || do { "BerkeleyDB"->can($name) } || do { require Errno; "Errno"->can($name) } or $self->_throw("BDB_CONST", "Sub $fullname is undefined") 0 0 0
Condition0%0/9
$caller->can($name) || $self->can($name) || do { 'BerkeleyDB'->can($name) } || do { require Errno; 'Errno'->can($name) }
l!l&&r!l&&!r
000
$caller->can($name) or $self->can($name) or do { "BerkeleyDB"->can($name) }
l!l&&r!l&&!r
000
$caller->can($name) or $self->can($name)
l!l&&r!l&&!r
000
MC/DC0%0/4
$caller->can($name) || $self->can($name) || do { 'BerkeleyDB'->can($name) } || do { require Errno; 'Errno'->can($name) }
$caller->can($name) $self->can($name) do { "BerkeleyDB"->can($name) } do { require Errno; 'Errno'->can($name) }
Truth table0%0/5
A or B or C || D
A= $caller->can($name)B= $self->can($name)C= do { "BerkeleyDB"->can($name) }D= do { require Errno; 'Errno'->can($name) }
ABCDresult
1XXX1
01XX1
001X1
00011
00000
246 or $self->_throw(BDB_CONST, qq(Sub $fullname is undefined));
247
248 # Now that we have a coderef, try calling it to get the error code.
249 # Catch any exceptions and repackage them into an error object.
250 0 ▶ my $return = $self->_try(sub { $self->_wrap($func) }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 my ($error) = $_ =~ /^(.*?)(?: at .+ line \d+)?\.?$/m;
252 0 $self->_throw(BDB_CONST, qq(Sub $fullname died "$error"));
253 0 });
254
255 # Make sure what we got is an integer. (Well, this doesn't actually go
256 # that far, but it's in the ballpark.)
257 0 ▶ int $return or $self->_throw(
Branch: 1 branch
BranchTrueFalseTotal
int $return or $self->_throw("BDB_CONST", qq[Sub $fullname returned non-integer "$return"]) 0 0 0
258 BDB_CONST, qq(Sub $fullname returned non-integer "$return"),
259 );
260
261 0 $return;
262 }
263
264 #
265 # Lookup a system error name from its integer errno code.
266 # (Complement to _const. Internal method, used by _exception)
267 #
268 # Used by _exception to show a user-friendly/googleable error name
269 # instead of an integer errno. Creates a hash mapping all the exportable
270 # POSIX constants from Errno. There are a lot, so we delay doing this until
271 # needed, then cache it.
272 #
273 0 ▶sub _lookup {
Subroutine _lookup: not called CC=5 SCAR=30.8
274 0 my ($self, $error) = @_;
275 0 my $code = int $error;
276
277 0 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 0 0 0
278 0 require Errno;
279
280 my $posix = (our $Posix ||= {
281 0 map { Errno->$_ => $_ } @{$Errno::EXPORT_TAGS{POSIX}}
282 0 ▶ })->{$code};
Condition0%0/2
our $Posix ||= {map({"Errno"->$_, $_;} @{$Errno::EXPORT_TAGS{"POSIX"};})}
l!l
00
MC/DC0%0/1
our $Posix ||= {map({"Errno"->$_, $_;} @{$Errno::EXPORT_TAGS{"POSIX"};})}
our $Posix
Truth table0%0/2
A
A= our $Posix
Aresult
11
00
283 0 ▶ return $posix if $posix;
Branch: 1 branch
BranchTrueFalseTotal
if $posix 0 0 0
284
285 0 local $! = $code;
286 0 ▶ my @name = grep $!{$_}, keys %!;
Subroutine BEGIN: called CC=1 SCAR=0.2
287 0 ▶ return $name[0] if @name == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @name == 1 0 0 0
288
289 # Otherwise, if @name > 1, the errno is ambigious because multiple
290 # errors share the same code. Many do, so not a frivolous check.
291 }
292
293 0 $self->_warn(qq(Can't resolve error code "$code"));
294 0 BDB_UNKNOWN;
295 }
296
297 #
298 # Walk down the callstack until we get the first package that isn't us.
299 # (Internal method, used by _const and _exception)
300 #
301 0 ▶sub _caller {
Subroutine _caller: not called CC=4 SCAR=30.0
302 0 my ($self, $frame) = @_;
303 0 my $base = $self->_Base;
304
305 0 my ($pkg, $file, $line, $sub);
306 0 ▶ while (($pkg, $file, $line, $sub) = (caller $frame++)[0..3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = (caller $frame++)[0..3]) 0 0 0
307 0 ▶ last if $pkg !~ /$base/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg =~ /$base/ 0 0 0
308 }
309
310 # Something went wrong.
311 # Don't $self->_warn again or we'll end up back here.
312 0 ▶ warn qq(Can't figure out who called into $base) unless $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 0 0
313 0 { package => $pkg, file => $file, line => $line, sub => $sub };
314 }
315
316 #
317 # Get a stack trace, excluding packages that belong to this distribution.
318 # (Internal method, used by _exception)
319 #
320 0 ▶sub _carp {
Subroutine _carp: not called CC=3 SCAR=20.8
321 0 my $self = shift;
322
323 0 ▶ our $Classes ||= do {
Condition0%0/3
our $Classes ||= do { no strict 'refs'; [map({$self->$_;} @{${"BerkeleyDB::Easy::Common::EXPORT_TAGS";}{"class"};})] }
l!l&&r!l&&!r
000
MC/DC0%0/2
our $Classes ||= do { no strict 'refs'; [map({$self->$_;} @{${"BerkeleyDB::Easy::Common::EXPORT_TAGS";}{"class"};})] }
our $Classes do { no strict 'refs'; [map({$self->$_;} @{${"BerkeleyDB::Easy::Common::EXPORT_TAGS";}{"class"};})] }
Truth table0%0/3
A ||= B
A= our $ClassesB= do { no strict 'refs'; [map({$self->$_;} @{${"BerkeleyDB::Easy::Common::EXPORT_TAGS";}{"class"};})] }
ABresult
1X1
011
000
324 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
325 0 [ map { $self->$_ } @{${_Common . q(::EXPORT_TAGS)}{class}} ];
326 };
327
328 0 require Carp;
329 0 local %Carp::Internal;
330 0 $Carp::Internal{$_}++ for @$Classes;
331 0 (my $trace = Carp::longmess()) =~ s/^\s+//;
332 0 $trace;
333 }
334
335 1 INFO and __PACKAGE__->_info(q(Error.pm finished loading));
336
337 1 1;
« Easy/Common.pm ↑ Summary Easy/Cursor.pm »