« blib/lib/DBIx/QuickORM/Manual/Dialects.pm
↑ Summary
blib/lib/DBIx/QuickORM/Manual/ForeignKeys.pm »
| 1 | package DBIx::QuickORM::Manual::Errors; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000033'; | ||
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =encoding UTF-8 | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | DBIx::QuickORM::Manual::Errors - Database errors, their kinds, and retrying, by example. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | A DBI error on the ORM's connected handle is thrown as a | ||
| 18 | L<DBIx::QuickORM::Exception::DB> object that says what kind of error it was | ||
| 19 | and whether trying again may help. The ORM's own complaints (a misused | ||
| 20 | method, a refused write) are plain strings. A C<forked> query reports its | ||
| 21 | child's error as text (L<DBIx::QuickORM::Manual::Background>). | ||
| 22 | |||
| 23 | =head1 SCHEMA | ||
| 24 | |||
| 25 | The examples use PostgreSQL and this schema: | ||
| 26 | |||
| 27 | =for highlighter language=sql | ||
| 28 | |||
| 29 | CREATE TABLE users ( | ||
| 30 | id SERIAL PRIMARY KEY, | ||
| 31 | name TEXT NOT NULL, | ||
| 32 | email TEXT UNIQUE, | ||
| 33 | prefs JSON, | ||
| 34 | referred_by INTEGER REFERENCES users (id), | ||
| 35 | created TIMESTAMP NOT NULL DEFAULT now() | ||
| 36 | ); | ||
| 37 | |||
| 38 | CREATE TABLE posts ( | ||
| 39 | id SERIAL PRIMARY KEY, | ||
| 40 | user_id INTEGER NOT NULL REFERENCES users (id), | ||
| 41 | title TEXT NOT NULL, | ||
| 42 | views INTEGER NOT NULL DEFAULT 0 | ||
| 43 | ); | ||
| 44 | |||
| 45 | =head1 CATCHING A DATABASE ERROR | ||
| 46 | |||
| 47 | =for highlighter language=perl | ||
| 48 | |||
| 49 | use Scalar::Util qw/blessed/; | ||
| 50 | |||
| 51 | my $ann = $orm->insert(users => { | ||
| 52 | name => 'Ann', | ||
| 53 | email => 'ann@example.com', | ||
| 54 | }); | ||
| 55 | |||
| 56 | my $ok = eval { | ||
| 57 | $orm->insert(users => { | ||
| 58 | name => 'Ann Again', | ||
| 59 | email => 'ann@example.com', # the address is taken | ||
| 60 | }); | ||
| 61 | 1; | ||
| 62 | }; | ||
| 63 | my $err = $@; | ||
| 64 | unless ($ok) { | ||
| 65 | die $err unless blessed($err) && $err->isa('DBIx::QuickORM::Exception::DB'); | ||
| 66 | die $err unless ($err->kind // '') eq 'unique_violation'; | ||
| 67 | # tell the user the address is taken | ||
| 68 | } | ||
| 69 | |||
| 70 | =head1 WHAT AN ERROR CARRIES | ||
| 71 | |||
| 72 | my $kind = $err->kind; # 'unique_violation' | ||
| 73 | my $state = $err->state; # '23505': the SQLSTATE, when the driver reports one | ||
| 74 | my $native = $err->native; # the driver's own code | ||
| 75 | my $text = $err->errstr; # the driver's message | ||
| 76 | my $sql = $err->statement; # the INSERT that failed | ||
| 77 | my $message = "$err"; # DBI's message: matching $@ against DBI's text still works | ||
| 78 | |||
| 79 | # 0 here, 1 for a deadlock, serialization failure, or lock timeout, | ||
| 80 | # and undef when the kind is unknown | ||
| 81 | my $retriable = $err->retriable; | ||
| 82 | |||
| 83 | The kinds, the same on every database: C<unique_violation>, | ||
| 84 | C<foreign_key_violation>, C<not_null_violation>, C<check_violation>, | ||
| 85 | C<deadlock>, C<serialization_failure>, C<lock_timeout>, or undef when the | ||
| 86 | dialect cannot tell. | ||
| 87 | |||
| 88 | SQLite and the MySQL family report native codes rather than useful SQLSTATEs. | ||
| 89 | DuckDB reports the same code and SQLSTATE for every error, so the ORM | ||
| 90 | classifies its message (L<DBIx::QuickORM::Manual::Dialects/ERRORS>). | ||
| 91 | |||
| 92 | =head1 THE ORM'S OWN ERRORS | ||
| 93 | |||
| 94 | my $croaked = !eval { $orm->insert(users => {nope => 1}); 1 }; | ||
| 95 | my $plain = !ref($@); # true: a complaint about how the ORM was called is a plain string | ||
| 96 | |||
| 97 | =head1 UNKNOWN ERRORS | ||
| 98 | |||
| 99 | # a scalar reference: a handle over SQL you write | ||
| 100 | my $failed = !eval { $orm->handle(\'SELECT * FROM no_such_table')->all; 1 }; | ||
| 101 | |||
| 102 | # true: a syntax error, a missing table, a lost connection (retriable is undef too) | ||
| 103 | my $unknown = blessed($@) && !defined $@->kind; | ||
| 104 | |||
| 105 | =head1 DEADLOCKS AND SERIALIZATION FAILURES | ||
| 106 | |||
| 107 | my $post_id = $orm->insert(posts => { | ||
| 108 | user_id => $ann->field('id'), | ||
| 109 | title => 'Hello', | ||
| 110 | })->field('id'); | ||
| 111 | |||
| 112 | $orm->txn( | ||
| 113 | sub { | ||
| 114 | my $post = $orm->find(posts => $post_id); | ||
| 115 | $post->update({views => $post->field('views') + 1}); | ||
| 116 | }, | ||
| 117 | isolation => 'serializable', # a conflicting write is then a serialization failure | ||
| 118 | # the whole block runs again, in a new transaction, up to 3 more times | ||
| 119 | retry => 3, | ||
| 120 | ); | ||
| 121 | |||
| 122 | These errors mean the database gave up on the transaction, and running it | ||
| 123 | again usually succeeds, so C<retry> is the answer. C<< retry => 3 >> allows | ||
| 124 | four attempts. The last failure is rethrown, as is any error that is not | ||
| 125 | retriable. | ||
| 126 | |||
| 127 | The block must be safe to run again: anything it did outside the database | ||
| 128 | happens again too. | ||
| 129 | |||
| 130 | At the default C<read committed> level a read-then-write like this does not | ||
| 131 | fail at all: it silently overwrites. | ||
| 132 | L<DBIx::QuickORM::Manual::Transactions/RETRY> shows the alternatives. | ||
| 133 | |||
| 134 | =head1 AFTER AN ERROR IN A TRANSACTION | ||
| 135 | |||
| 136 | my $txn = $orm->txn; | ||
| 137 | my $dup = !eval { | ||
| 138 | $orm->insert(users => { | ||
| 139 | name => 'Dup', | ||
| 140 | email => 'ann@example.com', | ||
| 141 | }); | ||
| 142 | 1; | ||
| 143 | }; | ||
| 144 | |||
| 145 | # after an error every statement croaks until the transaction is rolled back | ||
| 146 | $txn->rollback; | ||
| 147 | |||
| 148 | $orm->txn(sub { | ||
| 149 | my $ok = eval { | ||
| 150 | $orm->txn(sub { # a savepoint: the error rolls back only this insert | ||
| 151 | $orm->insert(users => { | ||
| 152 | name => 'Dup', | ||
| 153 | email => 'ann@example.com', | ||
| 154 | }); | ||
| 155 | }); | ||
| 156 | 1; | ||
| 157 | }; | ||
| 158 | unless ($ok) { | ||
| 159 | my $err = $@; | ||
| 160 | # rethrow anything unexpected | ||
| 161 | die $err unless blessed($err) && $err->isa('DBIx::QuickORM::Exception::DB'); | ||
| 162 | die $err unless ($err->kind // '') eq 'unique_violation'; | ||
| 163 | } | ||
| 164 | $orm->insert(users => {name => 'Bob'}); # the transaction goes on | ||
| 165 | }); | ||
| 166 | |||
| 167 | A failed statement marks its transaction failed, on every database, even | ||
| 168 | when the error is caught: then only a rollback may follow, and a commit rolls | ||
| 169 | back and croaks. See | ||
| 170 | L<DBIx::QuickORM::Manual::Transactions/"A FAILED STATEMENT">. | ||
| 171 | |||
| 172 | A block's exception rolls its transaction back by itself and is rethrown. | ||
| 173 | With the object form, roll back yourself. The inner block above rolls back to | ||
| 174 | its savepoint and rethrows. A bare C<eval> would swallow every error, so keep | ||
| 175 | only the one you expect. | ||
| 176 | |||
| 177 | DuckDB has no savepoints, so the nested form croaks there: roll the whole | ||
| 178 | transaction back instead. See L<DBIx::QuickORM::Manual::Transactions>. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | 1; | ||
| 183 | |||
| 184 | __END__ |