Succinct MooseX::Declare method signature validation errors

后端 未结 2 1327
臣服心动
臣服心动 2021-02-05 23:15

I\'ve been a proponent of adopting Moose (and MooseX::Declare) at work for several months. The style it encourages will really help the maintainability of our codebase, but not

相关标签:
2条回答
  • 2021-02-06 00:01

    Method::Signatures::Modifiers is a package which hopes to fix some of the problems of MooseX::Method::Signatures. Simply use it to overload.

    use MooseX::Declare;
    use Method::Signatures::Modifiers;
    
    class Foo
    {
        method bar (Int $thing) {
            # this method is declared with Method::Signatures instead of MooseX::Method::Signatures
        }
    }
    
    0 讨论(0)
  • 2021-02-06 00:04

    I'm glad you like MooseX::Declare. However, the method signature validation errors you're talking about aren't really from there, but from MooseX::Method::Signatures, which in turn uses MooseX::Types::Structured for its validation needs. Every validation error you currently see comes unmodified from MooseX::Types::Structured.

    I'm also going to ignore the stack-trace part of the error message. I happen to find them incredibly useful, and so does the rest of Moose cabal. I'm not going to removed them by default.

    If you want a way to turn them off, Moose needs to be changed to throw exception objects instead of strings for type-constraint validation errors and possibly other things. Those could always capture a backtrace, but the decision on whether or not to display it, or how exactly to format it when displaying, could be made elsewhere, and the user would be free to modify the default behaviour - globally, locally, lexically, whatever.

    What I'm going to address is building the actual validation error messages for method signatures.

    As pointed out, MooseX::Types::Structured does the actual validation work. When something fails to validate, it's its job to raise an exception. This exception currently happens to be a string, so it's not all that useful when wanting to build beautiful errors, so that needs to change, similar to the issue with stack traces above.

    Once MooseX::Types::Structured throws structured exception objects, which might look somewhat like

    bless({
        type => Tuple[Tuple[Object,Int],Dict[z,Optional[Str],y,Optional[Str]]],
        err  => [
            0 => bless({
                type => Tuple[Object,Int],
                err  => [
                    0 => undef,
                    1 => bless({
                        type => Int,
                        err  => bless({}, 'ValidationError::MissingValue'),
                    }, 'ValidationError'),
                ],
            }, 'ValidationError::Tuple'),
            1 => undef,
        ],
    }, 'ValidationError::Tuple')
    

    we would have enough information available to actually correlate individual inner validation errors with parts of the signature in MooseX::Method::Signatures. In the above example, and given your (Int $id, Str :$z, Str :$y) signature, it'd be easy enough to know that the very inner Validation::MissingValue for the second element of the tuple for positional parameters was supposed to provide a value for $id, but couldn't.

    Given that, it'll be easy to generate errors such as

    http://files.perldition.org/err1.png

    or

    http://files.perldition.org/err2.png

    which is kind of what I'm going for, instead of just formatting the horrible messages we have right now more nicely. However, if one wanted to do that, it'd still be easy enough once we have structured validation exceptions instead of plain strings.

    None of this is actually hard - it just needs doing. If anyone feels like helping out with this, come talk to us in #moose on irc.perl.org.

    0 讨论(0)
提交回复
热议问题