On Fri, 04 Mar 2005 10:26:52 -0500, David Newman
<[EMAIL PROTECTED]> wrote:
>
> How is the error handling? I'm writing a CGI::App now with DBI and I
> find that most of my time is writing error handling routines. If this
> query fails rollback and display an error, otherwise continue, etc. etc.
> It is alot of work.
If you are using a transactional database, then there is a 'helper'
method described on the Class::DBI Wiki which simplifies commits and
rollbacks. It works like this:
my $user_data = ...
my @friends = ...
my ($user) = My::DB->do_transaction( sub {
my $data = shift;
my $friends = shift;
my $user = My::DB::User->create($data);
foreach my $friend (@$friends) {
$user->add_to_friends($friend);
}
return $user;
}, $user_data, [EMAIL PROTECTED] ) or do {
return $self->error_page("Database Error: ".My::DB->error);
};
This will automatically do a rollback or commit depending the success
of the anonymous subroutine. Note that you don't need to worry about
making sure that any of the calls succeed inside the transaction
subroutine. If an error occurs and the code dies, the error will be
caught, and the transaction will be rolled back.
You will need to do some form validation and such before this stage,
and if all your data tests have passed, then this transaction should
always succeed unless there was a mistake made somewhere along the
way. In other words, an error should only occur if you have made a
mistake in your data validation (string too long or some such), or the
database is inaccessible, or you made a mistake in the table
structure.
Cheers,
Cees
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]