Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve exception message from postgresql function

I have a trigger function on a table that runs on inserts which for certain circumstances will raise an exception.

I maintain an old Perl application running on Catalyst that creates a transaction and inserts rows on the table.

When the trigger function raises an exception, I'd like to be able to print out just the error message I throw and not any debugging information (database operation, context, perl file, etc).

So for example, if my function throws something like:

raise exception 'Item with id % cannot be shipped at this time.', new.id;

I would like to only see

Item with id 13 cannot be shipped at this time.

and not

DBIx::Class::Row::insert(): DBI Exception: DBD::Pg::st execute failed: ERROR: Item with id 13 cannot be shipped at this time. [for Statement "INSERT INTO ... at /home/../lib/Class/Controller/Inv.pm line 260

The perl code is currently something like

$c->model('Class')->schema->txn_do(sub {
    ...
    eval {
        $shipment->insert;
        1;
    } or do {
        $error = $@;
        last;
    };

    if ($error) {
        $c->stash->{error} = $error;
    }
);

Thank you.

like image 237
rhyek Avatar asked Sep 26 '26 21:09

rhyek


1 Answers

You could access the errstr() method of the database handle, which is what is what is passed to warn/die anyway

 warn $c->model('Class')->schema->storage->dbh->errstr();
like image 111
Len Jaffe Avatar answered Sep 29 '26 10:09

Len Jaffe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!