From: Toby Inkster Date: Thu, 25 Apr 2013 13:00:00 +0000 (+0100) Subject: die with a helpful error message if $TYPE_MAP{$isa}->() returned something useless X-Git-Tag: v1.002000~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=513a3b5dc0dcee0a0d2d1f00c18af8a6d1068ba8;p=gitmo%2FMoo.git die with a helpful error message if $TYPE_MAP{$isa}->() returned something useless --- diff --git a/Changes b/Changes index a2f8e03..a0f1dc9 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - throw a useful exception when typemap doesn't return a value - avoid localising @_ when not required for Sub::Quote - successfully inflate a metaclass for attributeless classes (RT#86415) - fix false default values used with non-lazy accessors diff --git a/lib/Moo/HandleMoose.pm b/lib/Moo/HandleMoose.pm index 3bcb725..15ab89e 100644 --- a/lib/Moo/HandleMoose.pm +++ b/lib/Moo/HandleMoose.pm @@ -56,7 +56,7 @@ sub inject_real_metaclass_for { my ($name) = @_; our %DID_INJECT; return Class::MOP::get_metaclass_by_name($name) if $DID_INJECT{$name}; - require Moose; require Moo; require Moo::Role; + require Moose; require Moo; require Moo::Role; require Scalar::Util; Class::MOP::remove_metaclass_by_name($name); my ($am_role, $meta, $attr_specs, $attr_order) = do { if (my $info = $Moo::Role::INFO{$name}) { @@ -109,6 +109,8 @@ sub inject_real_metaclass_for { my $tc = $spec{isa} = do { if (my $mapped = $TYPE_MAP{$isa}) { my $type = $mapped->(); + Scalar::Util::blessed($type) && $type->isa("Moose::Meta::TypeConstraint") + or die "error inflating attribute '$name' for package '$_[0]': \$TYPE_MAP{$isa} did not return a valid type constraint'"; $coerce ? $type->create_child_type(name => $type->name) : $type; } else { Moose::Meta::TypeConstraint->new( diff --git a/xt/moox-types.t b/xt/moox-types.t index 3f3eef5..59edbe4 100644 --- a/xt/moox-types.t +++ b/xt/moox-types.t @@ -39,4 +39,20 @@ is( 'External (MooseX::Types type) ok' ); +local $@; +eval q { + package Fooble; + use Moo; + my $isa = sub { 1 }; + $Moo::HandleMoose::TYPE_MAP{$isa} = sub { $isa }; + has barble => (is => "ro", isa => $isa); + __PACKAGE__->meta->get_attribute("barble"); +}; + +like( + $@, + qr{^error inflating attribute 'barble' for package 'Fooble': \$TYPE_MAP\{CODE\(\w+?\)\} did not return a valid type constraint}, + 'error message for incorrect type constraint inflation', +); + done_testing;