http://www.perlmonks.org?node_id=738404

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

Replies are listed 'Best First'.
Re: post mortem on DBI errors
by mje (Curate) 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