blib/lib/DBIx/Squirrel/Errors.pm

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