pg: More verbose error traces

Partly because some errors currently appeared to come from within FU::PG
itself, which is useless, and partly because it's common to wrap
database access methods, while that's exactly the kind of operation
where you *really* want to know where the error originated from.

(Source: too much time wasted debugging VNDB errors)
This commit is contained in:
Yorhel 2025-02-07 10:59:27 +01:00
parent 96aee880ce
commit 7c8473533d
4 changed files with 46 additions and 27 deletions

View file

@ -15,7 +15,7 @@ package FU::PG::conn {
};
package FU::PG::txn {
use Carp 'croak';
use Carp 'confess';
my $COUNTER = 0;
@ -27,19 +27,19 @@ package FU::PG::txn {
# 2: $parent, undef if this is a top-level transaction.
sub commit($t) {
croak "Unable to commit transaction that has already finished" if !$t->[1];
confess "Unable to commit transaction that has already finished" if !$t->[1];
$t->exec($t->[2] ? "RELEASE SAVEPOINT fupg_$t->[1]" : 'COMMIT');
$t->[1] = undef;
}
sub rollback($t) {
croak "Unable to rollback transaction that has already finished" if !$t->[1];
confess "Unable to rollback transaction that has already finished" if !$t->[1];
$t->exec($t->[2] ? "ROLLBACK TO SAVEPOINT fupg_$t->[1]" : 'ROLLBACK');
$t->[1] = undef;
}
sub txn($t) {
croak "Unable to create sub-transaction when current transaction has already finished" if !$t->[1];
confess "Unable to create sub-transaction when current transaction has already finished" if !$t->[1];
$COUNTER++;
$t->exec("SAVEPOINT fupg_$COUNTER");
$t->[0]->_set_cookie($COUNTER);
@ -80,7 +80,7 @@ FU::PG - Another PostgreSQL client module
$conn->q('INSERT INTO books (title) VALUES ($1)', 'Revelation Space')->exec;
for my ($id, $title) ($conn->q('SELECT * FROM books')->flat) {
for my ($id, $title) ($conn->q('SELECT * FROM books')->flat->@*) {
print "$id: $title\n";
}
@ -365,8 +365,6 @@ handling, that is still available:
Just don't try to use transaction objects and manual transaction commands at
the same time, that won't end well.
=back
=head2 Errors