Useful errors for Moose and MooseX::Declare
Asked Answered
N

3

8

Moose is very lovely, but sometimes simple typos can cause hair-raisingly exciting long stacktraces with, from my point of view, zero useful content.

So, are there any tools to interpret this exploding into something helpful?

In particular for classes using plain Moose, Moose+MooseX::Method::Signatures, and MooseX::Declare.

The tools only need to be helpful while developing to catch those typo or thinko problems that make things just not work.

=========================

Following suggestion below, I'm using this not-quite-a-module-yet which is reducing my headaches a little, more ideas welcome, though:

package MooseX::QuietCarping;
# Not actually a Moose thing, but helpful for Moose.
# calm Moose-internal stacktraces down a little
use Carp;

my %retain = ();
sub import {
    my $class = shift;
    $retain{$_}++ for @_;
}

CHECK {
    for (sort keys %INC) {
    s{\.pm$}{};
    s{[/\\]}{::}g; # CROSS PLATFORM MY ARSE
    next if $retain{$_};
    $Carp::Internal{$_}++ if /^(?:Class::MOP|Moose|MooseX)\b/
    }
    %retain = (); # don't need this no more
}

1;
Nickinickie answered 29/5, 2011 at 23:59 Comment(3)
Note that the Moose team also relies on community feedback on errors that could be caught. A stacktrace is often much more information than required, but it makes sure it usually isn't too little. A better error message is usually a welcomed feature request.Milk
@Milk You're probably correct, but I'd rather my software was designed for the users than for the authors.Pragmatic
I've now found this question along the same lines - seems that this is mostly in the 'wait and see' phase of development: #4341616Nickinickie
G
4

One way I experimented with some time ago is putting Moose related classes into %Carp::Internal hash, something like this:

$Carp::Internal{$_}++ for qw{
    Class::MOP
    Class::MOP::Attribute
    Class::MOP::Class
    ...
};

Such classes will be skipped in stack trace, making it more compact and emphasizing your own code.

You can find them by traversing %INC variable:

package Dummy;
use Moose;
use MooseX::Declare;
# use ....;

for (sort keys %INC) {
    s{\.pm$}{};
    s{/}{::}g;
    print "$_\n" if /^(Class::MOP|Moose|MooseX)\b/;
}
Gmt answered 30/5, 2011 at 9:0 Comment(2)
That's cunning. I'll use that, but it won't always localise the errors in my source.Nickinickie
This would make a fantastic module. hint hintPragmatic
L
2

I seem to recall seeing a PerlMonks post by stvn a week or two ago saying that they're working on improving the Moose error messages. I don't think there's anything currently available to clean the up, though.

Leffler answered 30/5, 2011 at 9:38 Comment(0)
C
2

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
    }
}
Chimp answered 31/12, 2011 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.