blib/lib/DBIx/QuickORM/Exception/DB.pm

filter: Statementstmt 80.6% 25 / 31 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 4 / 8 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 80.0% 8 / 10 Podpod 50.0% 2 / 4 totaltotal 71.1% 42 / 59 CC 7 SCAR 9.3
CC
7
Cov
72%
init
4.1
init
4.1
retriable
2.5
CRAP 2.5
« blib/lib/DBIx/QuickORM/Dialect/SQLite.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm »
1 package DBIx::QuickORM::Exception::DB;
2 259 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 259 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 259 our $VERSION = '0.000031';
6  
7 259 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
8 <kind
9 <state
10 <native
11 <errstr
12 <statement
13 <message
14 259 };
15  
16 259 ▶use overload '""' => sub { $_[0]->{+MESSAGE} }, bool => sub { 1 }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 =pod
19  
20 =encoding UTF-8
21  
22 =head1 NAME
23  
24 DBIx::QuickORM::Exception::DB - A classified database error.
25  
26 =head1 DESCRIPTION
27  
28 Every error the database reports on the ORM's handle is thrown as one of
29 these. C<kind> says what went wrong when the dialect can tell, and
30 C<retriable> whether running the transaction again may help.
31  
32 It stringifies to DBI's message, so code matching C<$@> against DBI's text
33 keeps working. The ORM's own complaints about how it was called stay plain
34 strings.
35  
36 =head1 SYNOPSIS
37  
38 use Scalar::Util qw/blessed/;
39  
40 my $ok = eval { $orm->insert(users => {email => $email}); 1 };
41 my $err = $@;
42 unless ($ok) {
43 die $err unless blessed($err) && $err->isa('DBIx::QuickORM::Exception::DB');
44 return already_registered($email) if ($err->kind // '') eq 'unique_violation';
45 die $err;
46 }
47  
48 =head1 ATTRIBUTES
49  
50 =over 4
51  
52 =item $kind = $err->kind
53  
54 What went wrong, as far as the dialect can tell from the error codes, or
55 undef: C<deadlock>, C<serialization_failure>, C<lock_timeout>,
56 C<unique_violation>, C<foreign_key_violation>, C<not_null_violation>, or
57 C<check_violation>.
58  
59 =item $sqlstate = $err->state
60  
61 The SQLSTATE the driver reported, or undef. Some drivers report a generic one
62 (C<S1000>, C<HY000>) for most errors.
63  
64 =item $code = $err->native
65  
66 The driver's native error code (DBI's C<err>).
67  
68 =item $text = $err->errstr
69  
70 The driver's error text (DBI's C<errstr>).
71  
72 =item $sql = $err->statement
73  
74 The SQL the handle last ran, when DBI knows it.
75  
76 =item $text = $err->message
77  
78 DBI's message, which the exception stringifies to. Defaults to C<errstr>, or
79 C<Unknown database error> without one.
80  
81 =back
82  
83 =cut
84  
85 50 ▶sub init {
Subroutine init: called CC=3 SCAR=14.2
Pod undocumented
86 0 ▶ my $self = shift;
Subroutine init: not called CC=3 SCAR=14.2
Pod undocumented
87 0 ▶ $self->{+MESSAGE} //= $self->{+ERRSTR} // 'Unknown database error';
Condition50%4/8
$self->{"message"} //= $self->{"errstr"} // "Unknown database error"
l!l
3911
$self->{"errstr"} // "Unknown database error"
l!l
110
$self->{"message"} //= $self->{"errstr"} // "Unknown database error"
l!l
00
$self->{"errstr"} // "Unknown database error"
l!l
00
MC/DC100%1/1
$self->{"message"} //= $self->{"errstr"} // "Unknown database error"
$self->{"message"}
MC/DC0%0/1
$self->{"message"} //= $self->{"errstr"} // "Unknown database error"
$self->{"message"}
Truth table100%2/2
A
A= $self->{"message"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"message"}
Aresult
11
00
88 }
89  
90 =head1 PUBLIC METHODS
91  
92 =over 4
93  
94 =item $bool = $err->retriable
95  
96 Whether running the transaction again may get past the error.
97  
98 1 for a deadlock, a serialization failure, or a lock timeout, 0 for the
99 constraint violations, and undef when the kind is unknown.
100  
101 =back
102  
103 =cut
104  
105 my %RETRIABLE = (
106 777 (map { $_ => 1 } qw/deadlock serialization_failure lock_timeout/),
107 259 (map { $_ => 0 } qw/unique_violation foreign_key_violation not_null_violation check_violation/),
108 );
109  
110 27 ▶sub retriable {
Subroutine retriable: called CC=2 SCAR=9.2
Pod documented
111 0 ▶ my $self = shift;
Subroutine retriable: not called CC=2 SCAR=9.2
Pod documented
112 0 my $kind = $self->{+KIND};
113 0 ▶ return undef unless defined $kind;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $kind 3 24 27
unless defined $kind 0 0 0
114 0 return $RETRIABLE{$kind};
115 }
116  
117 259 1;
118  
119 __END__
« blib/lib/DBIx/QuickORM/Dialect/SQLite.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm »