Moose Loose Ends Part the Fourth
Still error day here in the Moose-Pen
One thing I wanted to try and do was have a little more meaning in my error-messages much like I did yesterday when my error message put out something like this
Database::Accessor create( $db 'Class', $container 'Hash-Ref||Class||Array-Ref of [Hash-ref||Class]', $options 'Hash-Ref'); Error: Incorrect Usage: The $container 'Array-Ref' must contain only Hash-refs or Classes. $container=$VAR1 = [
1,
{
'last_name' => 1
},
bless( {
'first_name' => 'test',
'last_name' => 'test'
}, 'Data::Test' )
…
at least in the above I know what container has the problem. I still can clean that up a little as I do not like that $VAR there and I can get rid of that just by adding this in;
package Database::Accessor;
use Moose;
with qw(Database::Accessor::Types
Database::Accessor::Roles::Common);
…
use Carp 'confess';
use Data::Dumper;
++ $Data::Dumper::Terse = 1;
and now I have;
$container=[
1,
{
'last_name' => 1
},
bless( {
'first_name' => 'test',
'last_name' => 'test'
}, 'Data::Test' )
]
Which is nice and I think I can improve on that with this patch;
grep( !( ref($_) eq 'HASH' or blessed($_) ), @{$container} );
confess( $message
-- . " The \$container 'Array-Ref' must contain only Hash-refs or Classes. \$container="
-- . Dumper($container) )
-- if (scalar(@bad) );
++ if (scalar(@bad)){
++ my $count = scalar(@bad);
++ $message .= " The \$container 'Array-Ref' must contain only Hash-refs or Classes. Scalar value";
++ $message .= ($count <=1 ) ? " " : "s ";
++ $message .= join(',',@bad);
++ $message .= ($count <= 1) ? " is" : " are";
++ $message .= " not allowed in ="
++ . Dumper($container);
++ confess( $message);
++ }
and now I get
Database::Accessor create( $db 'Class', $container 'Hash-Ref||Class||Array-Ref of [Hash-ref||Class]', $options 'Hash-Ref'); Error: Incorrect Usage: The $container 'Array-Ref' must contain only Hash-refs or Classes. Scalar value 1 is not allowed in =[
1,
{
'last_name' => 1
},
bless( {
'first_name' => 'test',
'last_name' => 'test'
}, 'Data::Test' )
]
Lets see what else I can play with;
The next one I want to play with is when a user does something like this
my $da= Database::Accessor->new(
view => { name => 'People' },
elements => [
{
nane => 'id',}
...
{
name => 'first_name',
]});
a simple typo of 'nane; rather than 'name' in the first elements. Right now I get a rather obtuse;
Attribute (name) is required at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 227
First I though I could clean that up with a 'confess' but this is a 'Moose' require being hit at this line in Types;
...
else {
$object = Database::Accessor::Element->new( %{$hash} );
}
…
I could try this
eval {
$object = Database::Accessor::Element->new( %{$hash} );
};
confess($@);
but the result on error is;
at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 230
Database::Accessor::Types::_element_coerce('HASH(0x431f1e4)') called at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 199
Database::Accessor::Types::_right_left_coerce('ARRAY(0x43172dc)') called at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 131
Database::Accessor::Types::__ANON__('ARRAY(0x43172dc)') called at C:/Dwimperl/perl/site/lib/Moose/Meta/TypeCoercion.pm line 65
Moose::Meta::TypeCoercion::__ANON__('ARRAY(0x43172dc)') called at C:/Dwimperl/perl/site/lib/Moose/Meta/TypeCoercion.pm line 100
Moose::Meta::TypeCoercion::coerce('Moose::Meta::TypeCoercion=HASH(0x3a95264)', 'ARRAY(0x43172dc)') called at C:/Dwimperl/perl/site/lib/Moose/Meta/TypeConstraint.pm line 145
Moose::Meta::TypeConstraint::coerce('Moose::Meta::TypeConstraint::Parameterizable=HASH(0x39eec2c)', 'ARRAY(0x43172dc)') called at C:/Dwimperl/perl/site/lib/MooseX/Constructor/AllErrors/Role/Object.pm line 45
Class::MOP::Class:::around('CODE(0x30b1224)', 'Database::Accessor', 'HASH(0x431733c)') called at C:/Dwimperl/perl/site/lib/Class/MOP/Method/Wrapped.pm line 164
Moose::Meta::Class::__ANON__::SERIAL::18::_wrapped_BUILDARGS('Database::Accessor', 'HASH(0x431733c)') called at C:/Dwimperl/perl/site/lib/Class/MOP/Method/Wrapped.pm line 95
Moose::Meta::Class::__ANON__::SERIAL::18::BUILDARGS('Database::Accessor', 'HASH(0x431733c)') called at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 155
Class::MOP::Class:::around('CODE(0x348d934)', 'Database::Accessor', 'HASH(0x431733c)') called at C:/Dwimperl/perl/site/lib/Class/MOP/Method/Wrapped.pm line 164
Database::Accessor::_wrapped_BUILDARGS('Database::Accessor', 'HASH(0x431733c)') called at C:/Dwimperl/perl/site/lib/Class/MOP/Method/Wrapped.pm line 95
Database::Accessor::BUILDARGS('Database::Accessor', 'HASH(0x431733c)') called at C:/Dwimperl/perl/site/lib/Moose/Object.pm line 22
Moose::Object::new('Database::Accessor', 'HASH(0x431733c)') called at 31_elements.t line 54
Moose-Poop with the only useful info 'called at 31_elements.t line 54' being at the end of this line and I do not even see the error 'Attribute (name)' in that mess above.
Ok how about just a 'die' then;
Died at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 236.
All right not much to go on there. How about;
eval {
$object = Database::Accessor::Element->new( %{$hash} );
};
if ($@) {
confess($@);
}
and that gives me;
Attribute (name) is required at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 234
Back to square one but at least I can play with the $@ value;
Now I did try an back up using caller to see how far up the stack I could get but afters a few lookbacks I ran into
Moose::Meta::TypeCoercion, C:/Dwimperl/perl/site/lib/Moose/Meta/TypeCoercion.pm, 65
so I am back into Moose-Poop land.
Before I lost all hope on getting an easy solution I had a quick look on Map of Cpan and found 'MooseX::Exception::Base'
Something for tomorrow I guess but after reading though it I do not thing it is going to give me what I want. I also had a look at 'MooseX::Attribute::ValidateWithException ' but with these lines in the POD
- You are o.k. with your code breaking in a future Moose release.
- You are o.k. with re-writing any and all code that depends on this functionality, if a future Moose release is incompatible with this module.
I think I will stay away for now.
The last thing I did today was re-run my test suite and I had a fail fin '30_view.t';
not ok 6 - Got error Constructor object
and when I looked into it I found that I had comments out this line;
use MooseX::AlwaysCoerce;
# use MooseX::Constructor::AllErrors;
use MooseX::Privacy;
in Accessor.pm. Putting it back in fixed the problem.
I got curious about the Mooses and then did a quick google search and found my own post from any moons ago! It would seem I went over this before and sort of came up with a way to handle this type of validation error.
Oh well I better go an re-read that post and see how I should of done it in the first place.
Leave a comment