3

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.

rhyek
  • 1,790
  • 1
  • 19
  • 23

2 Answers2

4

Perhaps this substitution:

my $error = $@;
$error =~ s/^.*ERROR: (.*) \[for Statement.*$/$1/;
Len Jaffe
  • 3,442
  • 1
  • 21
  • 28
  • Yeah. I mean I thought about using a regex. Thought there might've been a more... I guess 'proper' way of doing it. – rhyek Aug 24 '12 at 21:41
1

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();
Len Jaffe
  • 3,442
  • 1
  • 21
  • 28