[Dbix-class] Re: Catching DBI errors, without eval?

Christopher H. Laco claco at chrislaco.com
Sun Nov 12 22:43:59 GMT 2006


Josef Karthauser wrote:
> On Sun, Nov 12, 2006 at 11:56:58AM +0000, Josef Karthauser wrote:
>> Is it possible to catch DBI errors, without having to wrap the code
>> within eval blocks?
>>
>> The specific place where I'm experiencing trouble is,
>>
>>     DELETE FROM contact WHERE ( contactid =3D ? AND owner =3D ? ): '1203=
', '7'
>>     DBD::mysql::st execute failed: Cannot delete or update a parent row:=
 a
>>     foreign key constraint fails at
>>     /usr/local/lib/perl5/site_perl/5.8.8/DBIx/Class/Storage/DBI.pm line =
771.
>>
>> which was generated from an $obj->delete call.
> =

> Answering my own question, it appears that answer is no.  Looking at
> _execute I see that it evokes ->throw_exception to report an error, say
> when an SQL statement has failed because of a constraint.
> =

> There doesn't appear to be any facility to catch exceptions built in.
> Is that correct?
> =

> What I'd like to do is call
> =

>     $object->delete
> =

> and have it return a success or failure status.  I'm surprised that
> nobody appears to require this kind of behaviour.
> =

> Joe

What version are you on?
The upcoming 0.08 release (or -current from svn) has exception_action(),
which allow onw to direct exceptions to another sub for processing.

In previous versions, you could also set the HandleError option in the
connection information, or even set it on the active dbh:

> $schema_instance->storage->dbh->{HandleError} =3D sub process_error();

-=3DChris

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://jules.scsys.co.uk/pipermail/dbix-class/attachments/20061112/03=
8e5113/signature.pgp


More information about the Dbix-class mailing list