Perl 从postgresql函数检索异常消息

Perl 从postgresql函数检索异常消息,perl,postgresql,exception-handling,dbi,catalyst,Perl,Postgresql,Exception Handling,Dbi,Catalyst,我在一个表上有一个触发器函数,它运行在insert上,在某些情况下会引发异常 我维护一个在Catalyst上运行的旧Perl应用程序,它创建一个事务并在表中插入行 当trigger函数引发异常时,我希望能够打印出我抛出的错误消息,而不是任何调试信息(数据库操作、上下文、perl文件等) 例如,如果我的函数抛出如下内容: raise exception 'Item with id % cannot be shipped at this time.', new.id; 我只想看看 此时无法装运id

我在一个表上有一个触发器函数,它运行在insert上,在某些情况下会引发异常

我维护一个在Catalyst上运行的旧Perl应用程序,它创建一个事务并在表中插入行

当trigger函数引发异常时,我希望能够打印出我抛出的错误消息,而不是任何调试信息(数据库操作、上下文、perl文件等)

例如,如果我的函数抛出如下内容:

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

此时无法装运id为13的项目

而不是

DBIx::Class::Row::insert():DBI异常:DBD::Pg::st execute失败:错误:此时无法装运id为13的项目。[对于语句“插入…at/home/。/lib/Class/Controller/Inv.pm第260行

perl代码目前类似于

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

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

谢谢。

也许这个替代品:

my $error = $@;
$error =~ s/^.*ERROR: (.*) \[for Statement.*$/$1/;

也许这种替代:

my $error = $@;
$error =~ s/^.*ERROR: (.*) \[for Statement.*$/$1/;

您可以访问数据库句柄的errstr()方法,这就是传递给warn/die的方法

 warn $c->model('Class')->schema->storage->dbh->errstr();

您可以访问数据库句柄的errstr()方法,这就是传递给warn/die的方法

 warn $c->model('Class')->schema->storage->dbh->errstr();

是的。我的意思是我想用正则表达式。我想可能有一种更…我想“正确”的方法。是的。我的意思是我想用正则表达式。我想可能有一种更…我想“正确”的方法。