Shameful Moose

It is another shame day here in the Moose-pen

I never like taking out code but that is the first thing I had to do today. So all the code changes from yesterday have been rolled back and I still have the same problem;


Attribute (predicates) does not pass the type constraint because: Validation failed for 'Predicate' with value undef (not isa Database::Accessor::Predicate) at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 349# 
Namely an error message that tells me nothing of where my mistake;

$da->add_condition(  undef);
happened.

I take a good number of 'warn' statements throughout my 'Types' class finally find a spot where I can intercept and create a better error message.

I eventually tracked it down to the '_predicate_array_or_object' sub in the 'Types' class;


sub _predicate_array_or_object {
my ( $class, $in ) = @_;
my $objects = [];
foreach my $object ( @{$in} ) {
if ( ref($object) eq $class ) {
push( @{$objects}, $object );
}
elsif ( ref($object) eq "ARRAY" ) {
push(
@{$objects},
@{ _predicate_array_or_object( $class, $object ) }
);
}
else {
push( @{$objects}, $class->new( { predicates => $object } ) );
}
}
return $objects;
}

and my code was failing on the final 'else' as the $object that was coming in would have been 'undef' and I get that fail when I try to create a '$class' of 'Condition' with 'predicates' undef;

I added in these lines to capture that case;


else {
my ($package, $filename, $line, $subroutine) = caller(3);
++ die "Attribute (Condition) does not pass the type constraint because:
++ Validation failed for 'Conditions' with value undef"
++ unless($object);
push( @{$objects}, $class->new( { predicates => $object } ) );
}

and now I get

Attribute (Condition) does not pass the type constraint because:
Validation failed for 'Conditions' with value undef at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 346.

a little better. The only problem is I have introduced with the above a bug??

Can't use an undefined value as an ARRAY reference at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 1014.

when I am just trying to do this;

$da->retrieve( Data::Test->new(), $return );

after adding in a few 'dynamic_conditions'. Leaving that bug unfixed there will really make my code useless as I can't do any real work.

Well after poking about again I finally stumbled into it. At some point in my playing about I did this


has dynamic_links => (
isa => 'ArrayRefofLinks',
traits => ['Array','MooseX::MetaDescription::Meta::Trait'],
description => { not_in_DAD => 1 },
is => 'rw',
-- default => sub { [] },
++ #default => sub { [] },
handles => {
reset_links => 'clear',
add_link => 'push',
dynamic_link_count => 'count',
},
);

Must of had a brain cramp at some point. I took that '#' out and now all the failing tests pass;

Now to clean up that error message a little;

In this case I want to know where and what I am calling and I can get that with the good old 'caller' function. I will have to now do all of this in a 'unless' block like this;


unless ($object) {
my ( $package, $filename, $line, $subroutine ) = caller(3);
warn("$class $package, $filename, $line, $subroutine");
$subroutine =~ s/Database::Accessor:://g;
$class =~ s/Database::Accessor:://g;
my $die =
MooseX::Constructor::AllErrors::Error::Constructor->new(
caller => [ $package, $filename, $line, $subroutine ], );
$die->add_error(
MooseX::Constructor::AllErrors::Error::Misc->new(
{
message => "Database::Accessor "
. $subroutine
. " Error:\n"
. "You cannot add undef to dynamic_"
. $class . "s! "
}
)
);
die $die;
}
push( @{$objects}, $class->new( { predicates => $object } ) );

and now I get;

Database::Accessor add_condition Error:
You cannot add undef to dynamic_Conditions! at at 43_dynamic_conditions.t line 25

Just what the doctorb ordered.

Now I did do something a little naughty here as I am using the ' MooseX::Constructor::AllErrors' here to create an error for something that does not happen at the 'Constructor' stage. I suppose if some smarty pants out there will complain about it someday but I can suffer those slings and arrows;

Now I hope I can move on to something else soon.

jellied-moose-nose-cooking-native-american-food.jpeg

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