« blib/lib/DBIx/QuickORM/SchemaBuilder/Table/Temp.pm
↑ Summary
blib/lib/DBIx/QuickORM/Type/JSON.pm »
| 1 | package DBIx::QuickORM::Exception::DB; | ||||||||||||||||||||||||||||||
| 2 | 261 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 261 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | 261 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 261 | ▶ | 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 | 261 | }; | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 261 | ▶ | 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 { | ||||||||||||||||||||||||||||||
| 41 | $orm->insert( | ||||||||||||||||||||||||||||||
| 42 | users => { | ||||||||||||||||||||||||||||||
| 43 | name => $name, | ||||||||||||||||||||||||||||||
| 44 | email => $email, | ||||||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||
| 47 | 1; | ||||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||||
| 49 | my $err = $@; | ||||||||||||||||||||||||||||||
| 50 | unless ($ok) { | ||||||||||||||||||||||||||||||
| 51 | die $err unless blessed($err) && $err->isa('DBIx::QuickORM::Exception::DB'); | ||||||||||||||||||||||||||||||
| 52 | return already_registered($email) if ($err->kind // '') eq 'unique_violation'; | ||||||||||||||||||||||||||||||
| 53 | die $err; | ||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | =over 4 | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | =item $kind = $err->kind | ||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | What went wrong, as far as the dialect can tell from the error codes, or | ||||||||||||||||||||||||||||||
| 63 | undef: C<deadlock>, C<serialization_failure>, C<lock_timeout>, | ||||||||||||||||||||||||||||||
| 64 | C<unique_violation>, C<foreign_key_violation>, C<not_null_violation>, or | ||||||||||||||||||||||||||||||
| 65 | C<check_violation>. | ||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | =item $sqlstate = $err->state | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | The SQLSTATE the driver reported, or undef. Some drivers report a generic one | ||||||||||||||||||||||||||||||
| 70 | (C<S1000>, C<HY000>) for most errors. | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | =item $code = $err->native | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | The driver's native error code (DBI's C<err>). | ||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | =item $text = $err->errstr | ||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | The driver's error text (DBI's C<errstr>). | ||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | =item $sql = $err->statement | ||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | The SQL the handle last ran, when DBI knows it. | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | =item $text = $err->message | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | DBI's message, which the exception stringifies to. Defaults to C<errstr>, or | ||||||||||||||||||||||||||||||
| 87 | C<Unknown database error> without one. | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | =back | ||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | 69 | ▶ | sub init { | ||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $self->{+MESSAGE} //= $self->{+ERRSTR} // 'Unknown database error'; | ||||||||||||||||||||||||||||
|
Condition50%4/8
$self->{"message"} //= $self->{"errstr"} // "Unknown database error"
$self->{"errstr"} // "Unknown database error"
$self->{"message"} //= $self->{"errstr"} // "Unknown database error"
$self->{"errstr"} // "Unknown database error"
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"}
Truth table0%0/2
A
A= $self->{"message"}
| |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | =over 4 | ||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | =item $bool = $err->retriable | ||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | Whether running the transaction again may get past the error. | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | sub worth_retrying { | ||||||||||||||||||||||||||||||
| 109 | my ($err) = @_; | ||||||||||||||||||||||||||||||
| 110 | return 0 unless blessed($err) && $err->isa('DBIx::QuickORM::Exception::DB'); | ||||||||||||||||||||||||||||||
| 111 | return $err->retriable; | ||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | eval { $orm->insert(users => {email => 'nobody@example.com'}) }; | ||||||||||||||||||||||||||||||
| 115 | my $not_null = worth_retrying($@); # 0: users.name is NOT NULL, so it fails every time | ||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||
| 117 | eval { $orm->handle(\'SELECT * FROM no_such_table')->all }; | ||||||||||||||||||||||||||||||
| 118 | my $missing = worth_retrying($@); # undef: the dialect cannot classify it | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | C<< $orm->txn(sub { ... }, retry => $n) >> does this for you: it runs the whole | ||||||||||||||||||||||||||||||
| 121 | outermost block again, but only after an error whose C<retriable> is true. | ||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||
| 123 | 1 for a deadlock, a serialization failure, or a lock timeout, 0 for the | ||||||||||||||||||||||||||||||
| 124 | constraint violations, and undef when the kind is unknown. | ||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | =back | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | my %RETRIABLE = ( | ||||||||||||||||||||||||||||||
| 131 | 783 | (map { $_ => 1 } qw/deadlock serialization_failure lock_timeout/), | |||||||||||||||||||||||||||||
| 132 | 261 | (map { $_ => 0 } qw/unique_violation foreign_key_violation not_null_violation check_violation/), | |||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 26 | ▶ | sub retriable { | ||||||||||||||||||||||||||||
|
Subroutine
retriable: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
retriable: not called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 137 | 0 | my $kind = $self->{+KIND}; | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | return undef unless defined $kind; | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 139 | 0 | return $RETRIABLE{$kind}; | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | 261 | 1; | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||||