Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

concise Moose signature validation errors

by Boldra (Deacon)
on Oct 07, 2009 at 07:36 UTC ( [id://799669]=perlquestion: print w/replies, xml ) Need Help??

Boldra has asked for the wisdom of the Perl Monks concerning the following question:

I'm using MooseX::Declare (which uses MooseX::Method::Signatures (which uses Moose::Meta::TypeConstraint )) with long signatures such as in the following example:
use 5.010; use MooseX::Declare; class Manager { }; class Server { has 'hdr' => ( is => 'rw' ) }; class Archive { #.. other methods method init_archive ( Manager :$mgr!, Int :$debug, Str :$user!, Str :$pass!, Str :$path! where { $_ !~ /:/ and $_ =~ m{^[\\/]} }, HashRef :$target_args! where { ($_->{NR} and $_->{SID} and $_->{dt}) or ($_->{NR} and $_->{SID} and $_->{hostn +ame}) }, Server :$master where { defined $_->hdr } ) { say "ARCHIVE INITIALISED"; return $self; } } Archive->new->init_archive( user => 'boldra', pass => 'secret', path => 'd:\\data\\DYV\\', # path => '//hostname/d$/data/DYV', mgr => Manager->new, target_args => { NR => '01', SID => 1, hostname => 'localhost' }, master => Server->new( { hdr => 1 } ) + );
If you look closely, you'll see that I'm breaking the constraint on 'path', by supplying what looks like a local windows path, instead of the expected network path.

The problem you see when you run my example, is that the error message is a little too verbose to be useful, and somewhat defeats the purpose of having signatures:

Validation failed for 'MooseX::Types::Structured::Tuple[MooseX::Types: +:Structured::Tuple[Object],MooseX::Types::Structured::Dict[mgr,Manage +r,debug,MooseX::Types::Structured::Optional[Int],user,Str,pass,Str,pa +th,__ANON__,target_args,__ANON__,master,MooseX::Types::Structured::Op +tional[__ANON__]]]' failed with value [ [ Archive=HASH(0x2a012a8) ], +{ master => Server=HASH(0x2a7ce38), mgr => Manager=HASH(0x2a708e0), p +ass => "secret", path => "d:\data\DYV\", target_args => HASH(0x1aa3ad +8), user => "boldra" } ], Internal Validation Error is: Validation fa +iled for 'MooseX::Types::Structured::Dict[mgr,Manager,debug,MooseX::T +ypes::Structured::Optional[Int],user,Str,pass,Str,path,__ANON__,targe +t_args,__ANON__,master,MooseX::Types::Structured::Optional[__ANON__]] +' failed with value { master => Server={ hdr => 1 }, mgr => Manager={ + }, pass => "secret", path => "d:\data\DYV\", target_args => { NR => + 01, SID => 1, hostname => "localhost" }, user => "boldra" } at /usr/ +lib/perl5/site_perl/5.10/MooseX/Method/Signatures/Meta/Method.pm line + 445 MooseX::Method::Signatures::Meta::Method::validate('MooseX::Method +::Signatures::Meta::Method=HASH(0x2a7cec8)', 'ARRAY(0x29e1788)') call +ed at /usr/lib/perl5/site_perl/5.10/MooseX/Method/Signatures/Meta/Met +hod.pm line 145 Archive::init_archive('Archive=HASH(0x2a012a8)', 'user', 'boldra', + 'pass', 'secret', 'path', 'd:\data\DYV\', 'mgr', 'Manager=HASH(0x2a7 +08e0)', ...) called at /home/boldra/archive_test.pl line 40
After looking at the source of Moose::Meta::TypeConstraint, it looks like it's equipped to provide quite precise messages (including looking for a message method on the class type). So here's the questions:
  1. Am I doing something wrong in my signatures? What would I change in the code above to get a clear message as which argument is wrong and where?
  2. If this is a shortcoming of MooseX::Method::Signatures, but it's not because of Moose::Meta::TypeConstraint, where should the fix be? The MooseX::Method::Signature doc (BUGS, CAVEATS) suggests that a bug should be raised if the messages are not good. But which module really has the bug?
  3. Is there perhaps an easier way to get what I'm after (precise error messages on subroutine/method validation) with a different module?
Thanks for reading this far!

(Bart suggested yesterday that I ask this on a Moose forum, but I thought I'd start here. Feel free to post links to other forums, cpanforums.com/dist/moose seems pretty dead)



- Boldra

Replies are listed 'Best First'.
Re: concise Moose signature validation errors
by stvn (Monsignor) on Oct 07, 2009 at 19:38 UTC
    The problem you see when you run my example, is that the error message is a little too verbose to be useful, and somewhat defeats the purpose of having signatures:

    Yes, this is currently the biggest issue with MooseX::Declare, the fact that it still exposes quite a bit of implementation details in the error messages and stack traces. To be fair, vanilla Moose has this issue too, but the layers are not as deep so it tends to be less confusing.

    After looking at the source of Moose::Meta::TypeConstraint, it looks like it's equipped to provide quite precise messages (including looking for a message method on the class type).

    Yes, his however is not available in the inline type constraints your creating with MooseX::Declare. I suggest you look into pre-declaring your complex/custom types using either MooseX::Types or the more vanilla Moose::Util::TypeConstraints. This will give you access to the message feature you are looking for.

    Am I doing something wrong in my signatures? What would I change in the code above to get a clear message as which argument is wrong and where?

    No, your not doing anything wrong, just MooseX::Declare is still a young module and so not as usable as it could be. What you are seeing is the guts of the method signature, which is modeled as tuples using MooseX::Types::Structured.

    If this is a shortcoming of MooseX::Method::Signatures, but it's not because of Moose::Meta::TypeConstraint, where should the fix be? The MooseX::Method::Signature doc (BUGS, CAVEATS) suggests that a bug should be raised if the messages are not good. But which module really has the bug?

    If there is a bug in message handling, it is in MooseX::Method::Signatures as the message handling in plain Moose (and more specifically Moose::Util::TypeConstratints and/or Moose::Meta::TypeConstraint) has been tested thoroughly and used quite successfully by many people over the past 3+ years.

    Is there perhaps an easier way to get what I'm after (precise error messages on subroutine/method validation) with a different module?

    I would first suggest pre-declaring the types (as I suggested above) and adding the message in there. If MooseX::Method::Signatures does not properly respect that, please submit a bug to RT. The authors/maintainers of MooseX::Method::Signatures are pretty actively working on those modules and tend to turn around bug fixes really quickly. Of course if you include a patch to fix that is even better, but even a simple test case would help speed up the process too.

    As for other modules, there is also MooseX::Params::Validate which is not as pretty as MooseX::Method::Signatures but works very well and tends to produce less noise in the error messages.

    Bart suggested yesterday that I ask this on a Moose forum, but I thought I'd start here. Feel free to post links to other forums, cpanforums.com/dist/moose seems pretty dead

    Yes, it is not really used by anyone in the Moose community. I have a subscription to it, but as you can see in the comments I have made, I steer people away from it when possible. The most active part of the Moose community is #moose on irc.perl.org, followed then by the mailing list (moose@perl.org) and then lastly by perlmonks (which is still mostly me answering questions, but it is by far more active then cpanforums).

    -stvn
      Thanks for all the detailed answers stvn. I already had one go at predefining types with M::U::TC, but with no success. I kept getting an error suggesting that I needed to call class_type, which I did, but the error didn't change whether or not I called class_type and defined the type inside or outside the class block.

      As for MooseX::Params::Validate, it looks good, but getting MooseX::Method::Signatures to work seems more worthwhile, just because of the similarity to perl6 signatures.

      I'll have another go with M::M::S next week. Right now I'm under pressure to produce a working demo, and I've pulled MooseX::Declare out of the code. If I can't get it to work, I'll be sure to submit a bug (if I can't manage a patch).



      - Boldra

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-23 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found