Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

post mortem on DBI errors

by redlemon (Hermit)
on Jan 23, 2009 at 08:11 UTC ( #738404=perlquestion: print w/ replies, xml ) Need Help??
redlemon has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a Catalyst application and find myself doing something like:

eval { $form->model->create }; if ($@ =~ /column (\w+) is not unique/) { $form->get_field($1) ->get_constraint({type => 'Callback'}) ->force_errors(1); $form->process; }

Basically, I'm parsing the raw error message from my DBI engine (DBD::SQLite in this case).

If I switched to a different engine, I would probably get a different error message, so this would fail. Is there a way to find out the type of error and the column name it failed on? I checked $@, because it's an exception, but it only has a 'msg' method.

Ideally, I would like to do something like:

eval { $form->model->create }; if ($e =~ Exception::Class->caught('DBI::Unique')) { $form->get_field($e->column) ->get_constraint({type => 'Callback'}) ->force_errors(1); $form->process; }

Is there some way to achieve this?

--
Lyon

Comment on post mortem on DBI errors
Select or Download Code
Re: post mortem on DBI errors
by mje (Deacon) on Jan 23, 2009 at 10:31 UTC

    Not really possible to do this as all databases are different and produce different errors. You can examine $dbh->state to get an error code back and that simplifies the test to a simple string comparison e.g., $dbh->state eq '24000', but you will have to collect those error states for each database you use.

Re: post mortem on DBI errors
by andye (Curate) on Jan 23, 2009 at 14:27 UTC
    redlemon, is that specific error the one you're looking for? Or was that just an example?

    All the btes, andye

      It's just an example. There are several things that can go wrong during database insertion or update, some of which depend on user input, so need to be registered as a form error.

      In those cases the you'd get a list of catches, plus a catch-all that would just dump the actual error message

      It would probably need a change right down at the DBD/DBI level, and the way the drivers register their error handlers

      --
      Lyon

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://738404]
Approved by citromatik
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-08-31 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls