blib/lib/DBIx/Squirrel/Errors.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 14 / 14 Conditioncond 85.7% 12 / 14 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 96.6% 117 / 121 CC 13 SCAR 10.5
CC
13
Cov
98%
at_caller
6.0
in_method
4.0
_moved
3.0
CRAP 2.9
« blib/lib/DBIx/Squirrel/Result.pm ↑ Summary blib/lib/DBIx/Squirrel/Results.pm »
1 33 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel::Errors;
4  
5 33 ▶use experimental qw(try);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 33 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Ref::Util qw(is_coderef is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 33 ▶use Ekorn::Pipeline::Errors qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 33 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 33 our $VERSION = 'v4.3.1';
16  
17 33 our @EXPORT_OK = qw(at_caller croakf failed in_method);
18  
19 # The packages Carp looks past, to croak at the line that called
20 # DBIx::Squirrel: the DBI's own, which call DBIx::Squirrel's methods, such
21 # as prepare from prepare_cached, Ekorn::Pipeline's, which runs
22 # DBIx::Squirrel's stages, and DBIx::Squirrel's. Each of them that croaks
23 # takes this list as its @CARP_NOT.
24 33 our @INTERNAL = (
25 qw(
26 DBI DBI::db DBI::st
27 DBD::_::common DBD::_::db DBD::_::st
28 DBIx::Squirrel DBIx::Squirrel::db DBIx::Squirrel::st
29 DBIx::Squirrel::Context DBIx::Squirrel::Errors DBIx::Squirrel::Gather
30 DBIx::Squirrel::Iterator DBIx::Squirrel::Placeholders
31 DBIx::Squirrel::Result DBIx::Squirrel::Results DBIx::Squirrel::Stop
32 DBIx::Squirrel::Sugar DBIx::Squirrel::Util
33 DBIx::Squirrel::Stages::Batch DBIx::Squirrel::Stages::Flow
34 DBIx::Squirrel::Stages::Rows DBIx::Squirrel::Stages::Taps
35 ),
36 @Ekorn::Pipeline::Errors::INTERNAL,
37 );
38 33 our @CARP_NOT = @INTERNAL;
39  
40 # The files at_caller has been called from. A warning located at a line
41 # of one of them is one the DBI gave there.
42 33 my %CALLERS;
43  
44 # Run a block calling the DBI, and return what it returns. If the DBI
45 # raises an error, as it does under RaiseError, at the line of the block,
46 # raise it again at the line that called DBIx::Squirrel. Any other error,
47 # such as an object, or a string from a HandleError callback, is raised
48 # again as it was. A warning the DBI gives, as it does under PrintError,
49 # is given at the line that called DBIx::Squirrel too, as plain DBI gives
50 # it.
51 #
52 # The handler of warnings is one sub, not a closure made for each call,
53 # as this runs for each statement executed; what it needs is in package
54 # variables, local to the call. When at_caller runs inside itself, as it
55 # does when the DBI calls a method of DBIx::Squirrel's, the inner call
56 # keeps the handler the outer one passes warnings on to.
57 #
58 # Each branch of the try returns or dies, which Perl::Critic can't see.
59 7931 ▶sub at_caller : prototype(&) ($code) { ## no critic (Subroutines::RequireFinalReturn)
Subroutine at_caller: called CC=6 SCAR=17.9
Pod documented
60 7931 my $file = (caller)[1];
61 7931 $CALLERS{$file} = 1;
62 7931 my $handler = $SIG{__WARN__};
63 7931 ▶ local our $OUTER
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_coderef($handler) && $handler == \&DBIx::Squirrel::Errors::_moved ? : 2323 5608 7931
Condition100%3/3
Ref::Util::XS::is_coderef($handler) and $handler == \&DBIx::Squirrel::Errors::_moved
!ll&&!rl&&r
1655922323
MC/DC100%2/2
Ref::Util::XS::is_coderef($handler) and $handler == \&DBIx::Squirrel::Errors::_moved
Ref::Util::XS::is_coderef($handler) $handler == \&DBIx::Squirrel::Errors::_moved
Truth table100%3/3
A and B
A= Ref::Util::XS::is_coderef($handler)B= $handler == \&DBIx::Squirrel::Errors::_moved
ABresult
0X0
100
111
64 = is_coderef($handler) && $handler == \&_moved ? $OUTER : $handler;
65 7931 local $SIG{__WARN__} = \&_moved;
66 7931 try {
67 7931 return $code->();
68 }
69 catch ($e) {
70 51 ▶ my($message, $at) = is_ref($e) ? () : _location($e);
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_ref($e) ? : 1 50 51
71 51 ▶ die $e ## no critic (ErrorHandling::RequireCarping)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at and $at eq $file 20 31 51
Condition100%3/3
defined $at and $at eq $file
!ll&&!rl&&r
31731
MC/DC100%2/2
defined $at and $at eq $file
defined $at $at eq $file
Truth table100%3/3
A and B
A= defined $atB= $at eq $file
ABresult
0X0
100
111
72 unless defined $at && $at eq $file;
73 31 croak $message;
74 }
75 }
76  
77 # Handle a warning given inside at_caller: give one located at a line of a
78 # file at_caller was called from at the line that called DBIx::Squirrel
79 # instead, and pass it on.
80 13 ▶sub _moved ($warning) {
Subroutine _moved: called CC=3 SCAR=11.0
81 13 my($message, $at) = _location($warning);
82 $warning = Carp::shortmess($message)
83 13 ▶ if defined $at && $CALLERS{$at};
Branch: 1 branch
BranchTrueFalseTotal
if defined $at and $CALLERS{$at} 10 3 13
Condition100%3/3
defined $at and $CALLERS{$at}
!ll&&!rl&&r
2110
MC/DC100%2/2
defined $at and $CALLERS{$at}
defined $at $CALLERS{$at}
Truth table100%3/3
A and B
A= defined $atB= $CALLERS{$at}
ABresult
0X0
100
111
84 13 _warn(our $OUTER, $warning);
85 13 return;
86 }
87  
88 # Return a message Perl located, without its location, and the file it
89 # was located at, or nothing if it has no location.
90 63 ▶sub _location ($message) {
Subroutine _location: called CC=1 SCAR=0.0
91 # Where Perl says a die or warn happened, and the input it was reading.
92 63 state $INPUT = qr( , \ <[^>]*> \ (?: line | chunk ) \ \d+ )x;
93 63 state $AT = qr( \A (.*) \ at \ (.+?) \ line \ \d+ $INPUT? \. \n \z )xs;
94 63 return $message =~ $AT;
95 }
96  
97 # Give a warning to the __WARN__ handler given, or, without one, warn it.
98 # A warn inside a handler, as this is, isn't handled again.
99 19 ▶sub _warn ($handler, $warning) {
Subroutine _warn: called CC=2 SCAR=6.9
100 19 ▶ return $handler->($warning) if is_coderef($handler);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_coderef($handler) 18 1 19
101 1 warn $warning; ## no critic (ErrorHandling::RequireCarping)
102 1 return;
103 }
104  
105 # Croak with a message made by sprintf from a format and its arguments,
106 # as croak itself would, at the same line: goto leaves no frame of its
107 # own for Carp to see. It's a sub without a signature, as goto passes @_.
108 123 ▶sub croakf { ## no critic (Subroutines::RequireArgUnpacking)
Subroutine croakf: called CC=1 SCAR=0.0
Pod documented
109 123 my $format = shift;
110 123 @_ = (sprintf $format, @_);
111 123 goto &Carp::croak;
112 }
113  
114 # Return the message the DBI raises when a handle's method fails, as
115 # "DBD::SQLite::db prepare failed: ...".
116 15 ▶sub failed ($h, $method) {
Subroutine failed: called CC=2 SCAR=7.2
Pod documented
117 15 ▶ return "$h->{ImplementorClass} $method failed: @{[ $h->errstr // '' ]}";
Condition50%1/2
$h->errstr // ""
l!l
150
MC/DC0%0/1
$h->errstr // ""
$h->errstr
Truth table50%1/2
A
A= $h->errstr
Aresult
11
00
118 }
119  
120 # Run code as a method of a database handle, and return what it returns.
121 # An error the DBI raises for a method the code calls, such as prepare or
122 # execute, is raised again as the DBI raises one for the handle's method,
123 # as "DBD::SQLite::db do failed: ...", as plain DBI does, and so is a
124 # warning the DBI gives for one, as it does under PrintError.
125 #
126 # Its handler of warnings is one sub, as at_caller's is. Inside itself,
127 # the outer call's method is the one named, as plain DBI names the method
128 # called.
129 410 ▶sub in_method ($dbh, $method, $code) { ## no critic (Subroutines::RequireFinalReturn)
Subroutine in_method: called CC=4 SCAR=13.9
Pod documented
130 410 my $handler = $SIG{__WARN__};
131 local our @METHOD
132 410 ▶ = is_coderef($handler) && $handler == \&_renaming
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_coderef($handler) && $handler == \&DBIx::Squirrel::Errors::_renaming ? : 1 409 410
Condition66%2/3
Ref::Util::XS::is_coderef($handler) and $handler == \&DBIx::Squirrel::Errors::_renaming
!ll&&!rl&&r
04091
MC/DC50%1/2
Ref::Util::XS::is_coderef($handler) and $handler == \&DBIx::Squirrel::Errors::_renaming
Ref::Util::XS::is_coderef($handler) $handler == \&DBIx::Squirrel::Errors::_renaming
Truth table66%2/3
A and B
A= Ref::Util::XS::is_coderef($handler)B= $handler == \&DBIx::Squirrel::Errors::_renaming
ABresult
0X0
100
111
133 ? @METHOD
134 : ($dbh, $method, $handler);
135 410 local $SIG{__WARN__} = \&_renaming;
136 410 try {
137 410 return $code->();
138 }
139 catch ($e) {
140 35 ▶ die $e ## no critic (ErrorHandling::RequireCarping)
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 1 34 35
141 if is_ref($e);
142 34 die _renamed($e, $dbh, $method); ## no critic (ErrorHandling::RequireCarping)
143 }
144 }
145  
146 # Handle a warning given inside in_method: name the method in_method
147 # was given, and pass it on.
148 6 ▶sub _renaming ($warning) {
Subroutine _renaming: called CC=1 SCAR=0.0
149 6 my($dbh, $method, $outer) = our @METHOD;
150 6 _warn($outer, _renamed($warning, $dbh, $method));
151 6 return;
152 }
153  
154 # Return a message the DBI gave for a method that failed, naming the
155 # handle's method given in its place.
156 40 ▶sub _renamed ($message, $dbh, $method) {
Subroutine _renamed: called CC=1 SCAR=0.0
157 40 state $DBI_FAILED = qr( \A DBD:: [\w:]+ :: (?: db | st ) \ \w+ \ failed: )x;
158 40 return $message
159 22 =~ s( $DBI_FAILED )("$dbh->{ImplementorClass} $method failed:")xer;
160 }
161  
162 33 1;
163 __END__
« blib/lib/DBIx/Squirrel/Result.pm ↑ Summary blib/lib/DBIx/Squirrel/Results.pm »