Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Keep It Simple, Stupid
 
PerlMonks  

Better way to catch Class::DBI constraint failures?

by Hero Zzyzzx (Curate)
on Aug 12, 2004 at 15:57 UTC ( #382321=perlquestion: print w/ replies, xml ) Need Help??
Hero Zzyzzx has asked for the wisdom of the Perl Monks concerning the following question:

Folks,

I've just started using the mindblowing Class::DBI and I have to say it's just about the coolest module ever. *

What I REALLY want to do is find a more elegant way of catching specific CDBI::constraint failures- I've overridden CDBI::_croak in my base class, but the info I get back isn't all that useful.

The %info hash returns a reference to the Namespace object and an example error message is:

Namespace namespace fails 'list' constraint with 'page'

Where "Namespace" is a CDBI subclass, the field is "namespace" and the constraint was: Namespace->constrain_column(namespace=>[qw/foo/]);

I can parse this error message, but I fear that I can't rely on the text of the error message in perpetuity and that different constraints may throw different errors now or at some other point in time.

The reason I want to catch constraint failures is because I want the constraint checking to act as my HTML form validation check: If the constraint check barfs, I want to re-output the form with an error message on the field that failed. Without an easy way to know which field failed, this won't be easy. Using CDBI constraints to check form values seems like a pretty good way to not repeat myself. . . but I need to give the user specific feedback about the failure.

Ideas? Thanks in advance.

* Assuming that perl modules can be cool in the first place. :)

-Any sufficiently advanced technology is
indistinguishable from doubletalk.

My Biz

Comment on Better way to catch Class::DBI constraint failures?
Select or Download Code
Re: Better way to catch Class::DBI constraint failures?
by tomhukins (Curate) on Aug 12, 2004 at 16:17 UTC

    I encountered this problem a few months ago and spent a little time investigating.

    To understand what's going on, take a look at the source for Class::DBI's add_constraint method. Constraints are just before_set triggers that, on failure, croak with the error message you observed.

    So, if you wrote a before_set_namespace trigger, you could behave as you choose on error.

    The particular problem I encountered was that I wanted to list all failed constraints. I used a combination of triggers and two layers Exception::Class events.

    I apologise if this seems a little vague: I don't have access to the code any more and don't recall exactly what I did.

    Oh, and of course Perl modules can be cool. Distrust anyone who claims otherwise.

      So, if you wrote a before_set_namespace trigger, you could behave as you choose on error.

      I'm probably being dense, but: How? I put in a before_set_namespace trigger and I was getting the same error message, just in a different way.

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

      My Biz

Re: Better way to catch Class::DBI constraint failures?
by Aristotle (Chancellor) on Aug 13, 2004 at 01:58 UTC

    Try having a look at Class::DBI::FromCGI instead. It uses CGI::Untaint in a separate step to validate arguments, so Class::DBI's constraint checking becomes a mere safety net.

    Makeshifts last the longest.

      I've checked that one out, thanks! One reason I wanted to use CDBI constraints is to have my form validation logic exist only in one place. "Don't repeat yourself" and all that pragmatic stuff.

      Anyway, I figure if I can just get Class::DBI to emit more meaningful exceptions from constraint failures I can catch those in an overridden CDBI::_croak. You can read more detail here on the CDBI mailing list.

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

      My Biz

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-04-17 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (446 votes), past polls