Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 92818
Status: resolved
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: POPEL [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 2.1204
Fixed in: 2.1206



Subject: Misleading error "class name X does not match class->name Y"
The real cause of the error is a missing required attribute. In a simple case, Moose outputs a reasonable error message: $ perl -e 'package C1; use Moose; has x => (is=>qw(rw),required=>1); package main; my $obj=C1->new' Attribute (x) is required at [...]/Moose/Exception.pm line 38 [...] However, in a slightly more complicated case, Moose outputs a completely misleading error: $ perl -e 'package C1; use Moose; has x => (is=>qw(rw),required=>1); package C2; use Moose; extends "C1"; package main; my $obj=C2->new' class_name (C2) does not match class->name (C1) at [...]/Moose/Exception.pm line 38 [...] Debugging such a message is really annoying (esp. if you just get the error traceback from a user without having access to his computer). I think it is a regression, but I am not able now to install an older Moose to check it. BTW: is there some trick how to do this (e.g. with perlbrew & backpan)?
Didn't you already file this as https://rt.cpan.org/Ticket/Display.html?id=92770 ? You didn't report what Moose version you were using, but I think if you try with 2.1204 you will find things significantly improved.
On Fri Feb 07 13:59:45 2014, ETHER wrote: Show quoted text
> Didn't you already file this as > https://rt.cpan.org/Ticket/Display.html?id=92770 ?
No, that was another problem which is solved now (thanks). however, this problem is still here in Moose 2.1204. Just try it perl -e 'package C1; use Moose; has x => (is=>qw(rw),required=>1); package C2; use Moose; extends "C1"; package main; my $obj=C2->new' The expected error message is "Attribute (x) is required". The current error message is "class_name (C2) does not match class->name (C1)", which makes no sense to me. Show quoted text
> You didn't report what Moose version you were using,
I filled "Broken in"=2.1204. (I know it is broken also in some previous versions. I updated to the newest version of Moose before reporting this problem.) Martin Popel
The 'AttributeIsRequired' error is there, but it's farther down in the stack trace, because the exception heirarchy itself runs into problems and then generates a 'ClassNamesDoNotMatch' exception on itself. The stack trace is correct in 2.1005. The second exception is at line 80 of Moose::Exception::AttributeIsRequired: if( $self->has_attribute_name && $self->has_attribute && ( $self->attribute->name ne $self->attribute_name ) ) { throw_exception( AttributeNamesDoNotMatch => attribute_name => $self->attribute_name, attribute => $self->attribute ); It looks like there's a bug lurking here in the exception system; digging further...
Actually it's the "class names do not match" exception that we're hitting in lib/Moose/Exception/AttributeIsRequired.pm -- because the attribute actually lives on C1, but we encountered the problem when dealing with C2. The problem is this code: sub _build_class { my $self = shift; if( $self->has_attribute ) { return $self->attribute->associated_class; } elsif( $self->is_class_name_set ) { return Class::MOP::class_of( $self->class_name ); } }; In this case, we're lifting the class name off of the attribute (C1) and comparing it to the class name of the instance (C2), then complaining that they don't match. I don't think this is the right thing to do - in the exception, we should always refer to the class name of the instance, not the class name where the attribute originated (unless we use *both* of them in the error message, which we don't). Cabal, what say ye?
This is fixed in 2.1206, being released today.