From: Matt S Trout Date: Wed, 18 Jul 2012 19:21:15 +0000 (+0000) Subject: error prefixes for coerce exceptions too X-Git-Tag: v1.000000~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2d592c35ea3afabf31fffb42ce4d7b1c57559f8f;p=gitmo%2FMoo.git error prefixes for coerce exceptions too --- diff --git a/Changes b/Changes index cc53ee8..d527a31 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - error prefixes for isa and coerce exceptions - unmark Moo and Moose as experimental since it's relatively solid now - convert isa and coerce info from external role attributes - clear method cache after metaclass generation to fix autoclean bug diff --git a/lib/Method/Generate/Accessor.pm b/lib/Method/Generate/Accessor.pm index e7eebb4..21b8924 100644 --- a/lib/Method/Generate/Accessor.pm +++ b/lib/Method/Generate/Accessor.pm @@ -284,7 +284,10 @@ sub generate_coerce { sub _generate_coerce { my ($self, $name, $value, $coerce) = @_; - $self->_generate_call_code($name, 'coerce', "${value}", $coerce); + $self->_generate_die_prefix( + "coercion for ${\perlstring($name)} failed: ", + $self->_generate_call_code($name, 'coerce', "${value}", $coerce) + ); } sub generate_trigger { @@ -306,17 +309,25 @@ sub generate_isa_check { ($code, delete $self->{captures}); } -sub _generate_isa_check { - my ($self, $name, $value, $check) = @_; - "{\n" +sub _generate_die_prefix { + my ($self, $prefix, $inside) = @_; + "do {\n" .' my $sig_die = $SIG{__DIE__} || sub { die $_[0] };'."\n" .' local $SIG{__DIE__} = sub {'."\n" - .' $sig_die->(ref($_[0]) ? $_[0] : q{isa check for '.perlstring($name).' failed: }.$_[0]);'."\n" + .' $sig_die->(ref($_[0]) ? $_[0] : '.perlstring($prefix).'.$_[0]);'."\n" .' };'."\n" - .$self->_generate_call_code($name, 'isa_check', $value, $check) + .$inside ."}\n" } +sub _generate_isa_check { + my ($self, $name, $value, $check) = @_; + $self->_generate_die_prefix( + "isa check for ${\perlstring($name)} failed: ", + $self->_generate_call_code($name, 'isa_check', $value, $check) + ); +} + sub _generate_call_code { my ($self, $name, $type, $values, $sub) = @_; if (my $quoted = quoted_from_sub($sub)) { diff --git a/t/accessor-coerce.t b/t/accessor-coerce.t index d3a41e1..4288b97 100644 --- a/t/accessor-coerce.t +++ b/t/accessor-coerce.t @@ -94,7 +94,7 @@ run_for 'Baz'; ); } -like exception { Biff->new(plus_three => 1) }, qr/could not add three!/, 'Exception properly thrown'; +like exception { Biff->new(plus_three => 1) }, qr/coercion for "plus_three" failed: could not add three!/, 'Exception properly thrown'; { package Foo2;