sub throw_error {
my $self = shift;
- my $inv = $self->_error_thrower;
- unshift @_, "message" if @_ % 2 == 1;
- unshift @_, attr => $self if ref $self;
- unshift @_, $inv;
- my $handler = $inv->can("throw_error"); # to avoid incrementing depth by 1
- goto $handler;
+ Moose::Util::throw(@_);
}
sub _inline_throw_error {
# XXX ugh
$inv = 'Moose::Meta::Class' unless $inv->can('_inline_throw_error');
- # XXX ugh ugh UGH
- my $class = $self->associated_class;
- if ($class) {
- my $class_name = B::perlstring($class->name);
- my $attr_name = B::perlstring($self->name);
- $args = 'attr => Class::MOP::class_of(' . $class_name . ')'
- . '->find_attribute_by_name(' . $attr_name . '), '
- . (defined $args ? $args : '');
- }
-
return $inv->_inline_throw_error($msg, $args)
}
. 'do { local $_ = ' . $value . '; '
. $message . '->(' . $value . ')'
. '}',
- 'data => ' . $value
+ 'class => "Moose::Exception::TypeConstraint"',
+ 'attribute_name => ' . $self->name,
+ 'type_name => ' . $self->type_constraint->name,
+ 'value => ' . $value,
) . ';',
'}',
);
. 'do { local $_ = ' . $value . '; '
. $message . '->(' . $value . ')'
. '}',
- 'data => ' . $value
+ 'class => "Moose::Exception::TypeConstraint"',
+ 'attribute_name => ' . $self->name,
+ 'type_name => ' . $self->type_constraint->name,
+ 'value => ' . $value,
) . ';',
'}',
);
my $type_constraint = $self->type_constraint;
$type_constraint->check($val)
- || $self->throw_error("Attribute ("
+ || $self->throw_error(
+ superclass => 'Moose::Exception::TypeConstraint',
+ message => "Attribute ("
. $self->name
. ") does not pass the type constraint because: "
- . $type_constraint->get_message($val), data => $val, @_);
+ . $type_constraint->get_message($val),
+ value => $val,
+ attribute_name => $self->name,
+ type_name => $type_constraint->name,
+ @_);
}
package Moose::Meta::Attribute::Custom::Moose;
To check a value against a type constraint before setting it, fetch the
attribute instance using L<Class::MOP::Class/find_attribute_by_name>,
fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
-and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Recipe4>
+and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Company_Subtypes>
for an example.
=back