Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

[more detail added] Trying to get DBIx::Class::Result::Validation to work

by cLive ;-) (Prior)
on Sep 29, 2012 at 06:26 UTC ( [id://996341]=perlquestion: print w/replies, xml ) Need Help??

cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

Last night I was wiped, but here's an expanded version...

I wrote a standlone, minimal example, after reading this discussion (about the only one I could find on the module).

#!/usr/bin/perl use strict; use Overhaul::DB; my $dbx = Overhaul::DB->new('track'); my $schema = $dbx->schema; # this works as expected my $valid_result = $schema->resultset('track::Result')->create({ first_name => 'Clive', last_name => 'Testing', }); $valid_result->delete; my $bad_result = try { $schema->resultset('track::Result')->create({ last_name => 'Testing', }); } catch { $_ }; print "**************** We made it to here\n";

When I run it though, it dumps the error:

Overhaul::DB::Schema::Result::track::Result: Validation failed. { 'first_name' => [ 'First name is required' ] }

(yay, I wanted that). But then it dumps a stack trace and quits. Am I meant to overload the DBIx::Class exception handler?

at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 214 at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 208 DBIx::Class::Result::Validation::__ANON__('Overhaul::DB::Schem +a::Result::track::Result: Validation faile...') called at /usr/local/ +share/perl5/Try/Tiny.pm line 100 Try::Tiny::try('CODE(0x1e3ab10)', 'Try::Tiny::Catch=REF(0x1933 +640)') called at /usr/local/share/perl5/DBIx/Class/Result/Validation. +pm line 214 DBIx::Class::Result::Validation::_try_next_method('Overhaul::D +B::Schema::Result::track::Result=HASH(0x1e3ad98)', 'CODE(0xc591f0)', +undef) called at /usr/local/share/perl5/DBIx/Class/Result/Validation. +pm line 169 DBIx::Class::Result::Validation::insert('Overhaul::DB::Schema: +:Result::track::Result=HASH(0x1e3ad98)', undef) called at Overhaul/DB +/Schema/Result/track/Result.pm line 62 Overhaul::DB::Schema::Result::track::Result::insert('Overhaul: +:DB::Schema::Result::track::Result=HASH(0x1e3ad98)') called at /usr/s +hare/perl5/DBIx/Class/ResultSet.pm line 2196 DBIx::Class::ResultSet::create('DBIx::Class::ResultSet=HASH(0x +1e3ad80)', 'HASH(0x1c4dcb8)') called at tmp.pl line 18

When I switch it out to an eval, it works:

my $bad_result; eval { $bad_result = $schema->resultset('track::Result')->create({ last_name => 'Testing', }); };

$@ contains:

Create failed: Overhaul::DB::Schema::Result::track::Result: Validatio +n failed. { 'first_name' => [ 'First name is required' ] } at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 214

However, I still can't work out how to get to the error data (I'm guessing the module author is French, with limited English, so the docs are not that clear).

I think I'm missing something on the paradigm shift from plain DBI to DBIx::Class. In DBI, say the error was at the data level (wrong type, say), it would throw you like this (from the DB rather than internal validation):

$sth->execute || die "Query failed"

So, to sum up, I think it's two questions. What am I missing here? Is this the best way to run DBIx::Class data validation, or should I be using a different approach?

Replies are listed 'Best First'.
Re: Trying to get DBIx::Class::Result::Validation to work
by moritz (Cardinal) on Sep 29, 2012 at 07:46 UTC

    I've already said that in CB, but repeating it here for the benefit of others:

    the error is getting set as expected, but it croaks rather than gets encapsulated somewhere

    That part puzzles me. If a validation fails, I do expect it to throw an error. That gives you the opportunity to catch the exception wherever you want in your code, and deal with it there.

    If it didn't throw the exception, but encapsulated it somewhere, would the operation just fail silently if the validation failed? And what would the calling code make of that? That doesn't strike me as a useful modus operandi.

    (Disclaimer: I've never worked with dbic::Result::Validation, I'm merely applying my common sense here).

    Update: Answer to a new question after OP's update:

    However, I still can't work out how to get to the error data

    I'd guess that the exception is an objet of type DBIx::Class::Result::Validation::VException, and you can use the message and object accessors.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://996341]
Approved by Athanasius
Front-paged by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-18 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found