Moose Loose Ends Part the XIII

Its discovery day here in the Moose-Pen

I was all ready to wrap up my 'new' constructor errors when I figured I better see what I get when I do something like this;


$da->reset_conditions();
$da->add_condition( {
right => { value => 'test' },
operator => '=',
},);

which will fail as I have no 'left' on that 'condition' and that is what I got.

Attribute (left) is required at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 119 Looks like your test exited with 255 just after 16.

which I would like to clean up a bit as it does not tell me much. Digging deeper I found that it was this coercion that was throwing the error;

coerce 'Predicate', from 'HashRef',
via {
Database::Accessor::Predicate->new( %{$_} );
};

What I would like to get is something like on the 'new' constructor so the above fail should be;

'Database::Accessor add_condition Error:
The following Attribute is required: (conditions->left)

With constructor hash:
{
right => { value => 'test' },
operator => '=',
}

Now I am lucky here as I still have the 'AllErrors' object when I produce an error, I though that would only appear with a 'new'. Anyway with that object I can capture all of the validation errors in one like on the 'around BUILDARGS'

First things first a test;


$tests = {
condition => [
{
caption => 'Left is required',
exception => "\(conditions->left\)",
condition => {
leftx => {
name => 'last_name',
view => 'People'
},
right => { value => 'test' },
operator => '=',
}
},
...
],
};

$da = Database::Accessor->new($in_hash);

foreach my $in_key ( sort( keys( %{$tests} ) ) ) {
foreach my $test ( @{ $tests->{$in_key} } ) {
my $reset = "reset_$in_key”.”s";
my $add = "add_$in_key";
$da->$reset();
like(
exception { $da->$add($test->{$in_key})},
qr /$test->{exception}/,
"$test->{caption}; $in_key"
);
}
}


Much like the one from the other day now I just do a reset_X. and add_X. Now for some code;

coerce 'Predicate', from 'HashRef',
via {
# Database::Accessor::Predicate->new( %{$_} );
my $object;
my @hold = caller(10);
eval {
$object = Database::Accessor::Predicate->new( %{$_} )
};
if ($@) {
if (exists($ENV{'DA_ALL_ERRORS'}) and $ENV{'DA_ALL_ERRORS'} ){
die $@;
}
else {
my @errors;
my $error_msg;
my $error;
if ($@->missing()) {
foreach my $error ($@->missing()){
# warn(Dumper($error->attribute->documentation));
push(@errors,sprintf('%s%s'
,($error->attribute->documentation) ? $error->attribute->documentation."->" : ""
,$error->attribute->name()));
}

$error = "The following Attribute"
._is_are_msg(scalar(@errors))
."required: ("
.join(",",@errors)
.")\n";
}
if ($@->invalid()) {
@errors= ();
foreach my $error ($@->invalid()){
push(@errors, sprintf("'%s%s' Constraint: %s"
,($error->attribute->documentation) ? $error->attribute->documentation."->" : ""
,$error->attribute->name
,$error->attribute->type_constraint->get_message($error->data)
));
}
$error .= "The followling Attribute"
._did_do_msg(scalar(@errors))
." not pass validation: \n"
.join("\n",@errors);
}

my $misc = "Database::Accessor new Error:\n"
. $error
. "\nWith constructor hash:\n"
. Dumper($_);
my $die = MooseX::Constructor::AllErrors::Error::Constructor->new(
caller => [ @hold ],
);
$die->add_error(MooseX::Constructor::AllErrors::Error::Misc->new({message =>$misc}));
die $die;
}
}
return $object;
};

sub _is_are_msg {
my ($count) = @_;
return "s are "
if ($count >1);
return " is "
}

sub _did_do_msg {
my ($count) = @_;
return "s do "
if ($count >1);
return " did "
}

Now as you can see that is a 99.7% steal from the 'around' and end then result is;

Database::Accessor new Error:
The following Attribute is required: (conditions->left)

With constructor hash:
{
'operator' => '=',
'right' => {
'value' => 'test'
},
}
at native delegation method Database::Accessor::add_condition (push) of attribute dynamic_conditions (defined at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 518) line 5#


Ok some cleaning up and re-factoring to do but I think I have this settled now. Not going to bore you with all of that at least not today.

VCR500455732_hd.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations