From: Hans Dieter Pearcey Date: Thu, 2 Jul 2009 18:09:17 +0000 (-0700) Subject: more useful error message for delegation w/ unblessed value X-Git-Tag: 0.86~1^2~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ad5e9d0d22cc2da6d7533c650acba928b615a5d8;p=gitmo%2FMoose.git more useful error message for delegation w/ unblessed value --- diff --git a/Changes b/Changes index a620ca0..8a322e6 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,11 @@ Also see Moose::Manual::Delta for more details of, and workarounds for, noteworthy changes. +0.86 + * Moose::Meta::Method::Delegation + - Delegation now dies with a more useful error message if the + attribute's accessor returns something defined but unblessed. (hdp) + 0.85 Fri, Jun 26, 2009 * Moose::Meta::Attribute - The warning for 'no associated methods' is now split out into diff --git a/lib/Moose/Meta/Method/Delegation.pm b/lib/Moose/Meta/Method/Delegation.pm index 28b7a15..aa146f4 100644 --- a/lib/Moose/Meta/Method/Delegation.pm +++ b/lib/Moose/Meta/Method/Delegation.pm @@ -88,6 +88,15 @@ sub _initialize_body { method_name => $method_to_call, object => $instance ); + ( blessed $proxy ) + || $self->throw_error( + "Cannot delegate $handle_name to $method_to_call because " + . "the value of " + . $self->associated_attribute->name + . " is not an object (got '$proxy')", + method_name => $method_to_call, + object => $instance + ); $proxy->$method_to_call(@_); }; } diff --git a/t/020_attributes/010_attribute_delegation.t b/t/020_attributes/010_attribute_delegation.t index 45505a1..84890b9 100644 --- a/t/020_attributes/010_attribute_delegation.t +++ b/t/020_attributes/010_attribute_delegation.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 88; +use Test::More tests => 89; use Test::Exception; @@ -410,3 +410,10 @@ is($car->stop, 'Engine::stop', '... got the right value from ->stop'); ok(!$i->meta->has_method('foo_bar'), 'handles method foo_bar is removed'); } +# Make sure that a useful error message is thrown when the delegation target is +# not an object +{ + my $i = Bar->new(foo => []); + throws_ok { $i->foo_bar } qr/is not an object \(got 'ARRAY/, + 'useful error from unblessed reference'; +}