blib/lib/DBIx/ParseError/MySQL.pm

filter: Statementstmt 100% 47 / 47 Branchbran 91.6% 11 / 12 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 86.7% 72 / 83 CC 10 SCAR 12.9
CC
10
Cov
90%
__ANON__
7.1
_build_error_type
5.0
_build_is_transient
2.0
CRAP 3.6
↑ Summary
1 package DBIx::ParseError::MySQL;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Types::Standard qw( Str Bool Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Error parser for MySQL
13 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our $VERSION = 'v1.0.7'; # VERSION
15  
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod use DBIx::ParseError::MySQL;
19 #pod
20 #pod eval {
21 #pod my $result = $dbh->do('SELECT 1');
22 #pod };
23 #pod if ($@) {
24 #pod if (DBIx::ParseError::MySQL->new($@)->is_transient) { $dbh->reconnect }
25 #pod else { die; }
26 #pod }
27 #pod
28 #pod =head1 DESCRIPTION
29 #pod
30 #pod This module is a database error categorizer, specifically for MySQL. This module is also
31 #pod compatible with Galera's WSREP errors. Errors from AWS Aurora failovers, like a demoted
32 #pod writer or write forwarding failures, are also recognized.
33 #pod
34 #pod =head1 ATTRIBUTES
35 #pod
36 #pod =head2 orig_error
37 #pod
38 #pod Returns the original, untouched error object or string.
39 #pod
40 #pod =cut
41  
42 2 has orig_error => (
43 is => 'ro',
44 isa => Str|Object,
45 required => 1,
46 );
47  
48 #pod =head2 error_string
49 #pod
50 #pod Returns the stringified version of the error.
51 #pod
52 #pod =cut
53  
54 2 has error_string => (
55 is => 'lazy',
56 isa => Str,
57 init_arg => undef,
58 );
59  
60 38 ▶sub _build_error_string {
Subroutine _build_error_string: called CC=1 SCAR=0.0
61 38 my $self = shift;
62  
63 # All of the exception objects should support this, too.
64 38 return $self->orig_error."";
65 }
66  
67 #pod =head2 error_type
68 #pod
69 #pod Returns a string that describes the type of error. These can be one of the following:
70 #pod
71 #pod lock Lock errors, like a lock wait timeout or deadlock
72 #pod connection Connection/packet failures, disconnections
73 #pod shutdown Errors that happen when a server is shutting down or failing over,
74 #pod like Galera/WSREP or Aurora read-only and write forwarding errors
75 #pod duplicate_value Duplicate entry errors
76 #pod unknown Any other error
77 #pod
78 #pod =cut
79  
80 2 has error_type => (
81 is => 'lazy',
82 isa => Str,
83 init_arg => undef,
84 );
85  
86 38 ▶sub _build_error_type {
Subroutine _build_error_type: called CC=5 SCAR=16.1
87 38 my $self = shift;
88  
89 38 my $error = $self->error_string;
90  
91 # We have to capture just the first error, not other errors that may be buried in the
92 # stack trace.
93 38 $error =~ s/ at [^\n]+ line \d+\.?(?s:\n.*)?//;
94  
95 # Disable /x flag to allow for whitespace within string, but turn it on for newlines
96 # and comments.
97 #
98 # These error messages are purposely long and case-sensitive, because we're looking
99 # for these errors -anywhere- in the string. Best to get as exact of a match as
100 # possible.
101  
102 # Locks
103 38 ▶ return 'lock' if $error =~ m<
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ m[ (?-x:Deadlock found when trying to get (?:user-level |locking service )?lock; try )(?: (?-x:restarting transaction)| (?-x:rolling back transaction/releasing locks and restarting lock acquisition)| (?-x:releasing locks and restarting lock acquisition) )| (?-x:Lock wait timeout exceeded; try restarting transaction)| (?-x:Service lock wait timeout exceeded)| (?-x:Table definition has changed, please retry transaction)| (?-x:Prepared statement needs to be re-prepared)| (?-x:WSREP detected deadlock/conflict and aborted the transaction.\s+Try restarting the transaction) ]x 8 30 38
104 (?-x:Deadlock found when trying to get (?:user-level |locking service )?lock; try )(?:
105 (?-x:restarting transaction)|
106 (?-x:rolling back transaction/releasing locks and restarting lock acquisition)|
107 (?-x:releasing locks and restarting lock acquisition)
108 )|
109 (?-x:Lock wait timeout exceeded; try restarting transaction)|
110 (?-x:Service lock wait timeout exceeded)|
111 (?-x:Table definition has changed, please retry transaction)|
112 (?-x:Prepared statement needs to be re-prepared)|
113 (?-x:WSREP detected deadlock/conflict and aborted the transaction.\s+Try restarting the transaction)
114 >x;
115  
116 # Various connection/packet problems
117 30 ▶ return 'connection' if $error =~ m<
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ m[ # Connection dropped/interrupted (?-x:MySQL server has gone away)| (?-x:Lost connection to MySQL server)| # NOTE: Exclude max_execution_time interruptions, since these are not connection # failures, and retrying them would just produce the same results (?-x:Query execution was interrupted(?!, maximum statement execution time exceeded))| # A prepared statement handler unknown to the server means the connection's # server-side state is gone (eg the server restarted or failed over), so the # connection is effectively dead and the query should be retried (?-x:Unknown prepared statement handler \(\d+\) given to mysql_stmt_precheck)| # Initial connection failure (?-x:Bad handshake)| (?-x:Too many connections)| (?-x:Not enough connections on writer to handle your request)| (?-x:Host '\S+' is blocked because of many connection errors)| (?-x:Can't get hostname for your address)| (?-x:Can't connect to (?:local )?MySQL server)| # Packet corruption (?-x:Got a read error from the connection pipe)| (?-x:Got (?:an error|timeout) (?:reading|writing) communication packets)| (?-x:Got packets out of order)| (?-x:Couldn't uncompress communication packet)| (?-x:Malformed communication packet)| # XXX: This _might be_ a connection failure, but the DBD::mysql error message # does not expose the direct failure cause. See DBD::mysql/dbdimp.c#L2551. (?-x:Turning (?:off|on) AutoCommit failed) ]x 15 15 30
118 # Connection dropped/interrupted
119 (?-x:MySQL server has gone away)|
120 (?-x:Lost connection to MySQL server)|
121 # NOTE: Exclude max_execution_time interruptions, since these are not connection
122 # failures, and retrying them would just produce the same results
123 (?-x:Query execution was interrupted(?!, maximum statement execution time exceeded))|
124 # A prepared statement handler unknown to the server means the connection's
125 # server-side state is gone (eg the server restarted or failed over), so the
126 # connection is effectively dead and the query should be retried
127 (?-x:Unknown prepared statement handler \(\d+\) given to mysql_stmt_precheck)|
128  
129 # Initial connection failure
130 (?-x:Bad handshake)|
131 (?-x:Too many connections)|
132 (?-x:Not enough connections on writer to handle your request)|
133 (?-x:Host '\S+' is blocked because of many connection errors)|
134 (?-x:Can't get hostname for your address)|
135 (?-x:Can't connect to (?:local )?MySQL server)|
136  
137 # Packet corruption
138 (?-x:Got a read error from the connection pipe)|
139 (?-x:Got (?:an error|timeout) (?:reading|writing) communication packets)|
140 (?-x:Got packets out of order)|
141 (?-x:Couldn't uncompress communication packet)|
142 (?-x:Malformed communication packet)|
143  
144 # XXX: This _might be_ a connection failure, but the DBD::mysql error message
145 # does not expose the direct failure cause. See DBD::mysql/dbdimp.c#L2551.
146 (?-x:Turning (?:off|on) AutoCommit failed)
147 >x;
148  
149 # Failover/shutdown of node/server
150 15 ▶ return 'shutdown' if $error =~ m<
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ / (?-x:WSREP has not yet prepared node for application use)| (?-x:Server shutdown in progress)| (?-x:Normal shutdown)| (?-x:Shutdown complete)| (?-x:The MySQL server is running with the --(?:super-)?read-only option so it cannot execute this statement)| (?-x:Cannot execute statement in a READ ONLY transaction)| (?-x:Running in read-only mode)| (?-x:Forwarded connection on Writer terminated; try restarting transaction)| (?-x:Internal write forwarding error)| (?-x:Failed to send write forwarding request to writer) /x 9 6 15
151 (?-x:WSREP has not yet prepared node for application use)|
152 (?-x:Server shutdown in progress)|
153 (?-x:Normal shutdown)|
154 (?-x:Shutdown complete)|
155 (?-x:The MySQL server is running with the --(?:super-)?read-only option so it cannot execute this statement)|
156 (?-x:Cannot execute statement in a READ ONLY transaction)|
157 (?-x:Running in read-only mode)|
158 (?-x:Forwarded connection on Writer terminated; try restarting transaction)|
159 (?-x:Internal write forwarding error)|
160 (?-x:Failed to send write forwarding request to writer)
161 >x;
162  
163 # Duplicate entry error
164 6 ▶ return 'duplicate_value' if $error =~ m<
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ / # Any value can be in the first piece here... (?-x:Duplicate entry '.+?' for key '\S+') /sx 2 4 6
165 # Any value can be in the first piece here...
166 (?-x:Duplicate entry '.+?' for key '\S+')
167 >xs; # include \n in .+
168  
169 4 return 'unknown';
170 }
171  
172  
173 #pod =head2 is_transient
174 #pod
175 #pod Returns a true value if the error is the type that is likely transient. For example,
176 #pod errors that recommend retrying transactions or connection failures. This check can be
177 #pod used to figure out if it's worth retrying a transaction.
178 #pod
179 #pod This is merely a check for the following L<error types|/error_type>:
180 #pod C<< lock connection shutdown >>.
181 #pod
182 #pod =cut
183  
184 2 has is_transient => (
185 is => 'lazy',
186 isa => Bool,
187 init_arg => undef,
188 );
189  
190 38 ▶sub _build_is_transient {
Subroutine _build_is_transient: called CC=2 SCAR=6.9
191 38 my $self = shift;
192  
193 38 my $type = $self->error_type;
194  
195 38 ▶ return 1 if $type =~ /^(lock|connection|shutdown)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(lock|connection|shutdown)$/ 32 6 38
196 6 return 0;
197 }
198  
199 #pod =head1 CONSTRUCTORS
200 #pod
201 #pod =head2 new
202 #pod
203 #pod my $parsed_error = DBIx::ParseError::MySQL->new($@);
204 #pod
205 #pod Returns a C<DBIx::ParseError::MySQL> object. Since the error is the only parameter, it
206 #pod can be passed by itself.
207 #pod
208 #pod =for Pod::Coverage BUILDARGS
209 #pod
210 #pod =cut
211  
212 38 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=19.6
213 38 my ($orig, $class, @args) = @_;
214  
215 38 ▶ if (@args == 1 && defined $args[0] && (!ref $args[0] || blessed $args[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and defined $args[0] and !ref($args[0]) || builtin::blessed($args[0])) 38 0 38
Condition33%3/9
@args == 1 and defined $args[0] and !ref($args[0]) || builtin::blessed($args[0])
!ll&&!rl&&r
0038
@args == 1 and defined $args[0]
!ll&&!rl&&r
0038
not ref $args[0] or builtin::blessed($args[0])
l!l&&r!l&&!r
3800
MC/DC0%0/4
@args == 1 and defined $args[0] and !ref($args[0]) || builtin::blessed($args[0])
@args == 1 defined $args[0] not ref $args[0] builtin::blessed($args[0])
Truth table20%1/5
A and B and not(C) or D
A= @args == 1B= defined $args[0]C= not ref $args[0]D= builtin::blessed($args[0])
ABCDresult
0XXX0
10XX0
11000
111X1
11011
216 38 my $error = shift @args;
217 38 push @args, ( orig_error => $error );
218 }
219  
220 38 return $class->$orig(@args);
221 2 };
222  
223 #pod =head1 SEE ALSO
224 #pod
225 #pod L<DBIx::Class::ParseError> - A similar parser, but specifically tailored to L<DBIx::Class>.
226 #pod
227 #pod =cut
228  
229 2 1;
230  
231 __END__
↑ Summary