2012-08-23 53 views
2

檢索異常消息我對在插入其中的某些情況下會引發異常運行的表的觸發功能。從PostgreSQL函數

我保持催化劑上運行舊的Perl應用程序,創建了一個事務,並在表中插入行。

當觸發函數拋出一個例外,我希望能夠打印出剛錯誤消息我扔,而不是任何調試信息(數據庫操作,背景,perl的文件等)。

因此,舉例來說,如果我的函數拋出這樣的:

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

我想只看到

Item with id 13 cannot be shipped at this time.

,而不是

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

Perl代碼是目前東西像

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

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

謝謝。

回答

3

也許這個換人:

my $error = [email protected]; 
$error =~ s/^.*ERROR: (.*) \[for Statement.*$/$1/; 
+0

呀。我的意思是我想過使用正則表達式。認爲有可能已經更...我猜「適當的」做這件事的方式。 – rhyek

1

你可以訪問數據庫手柄,這是什麼是什麼是傳遞給警告的errstr()方法/死反正

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