From: Shawn M Moore Date: Mon, 18 Jun 2012 22:30:08 +0000 (-0400) Subject: Use Moose::Util::throw in lib/Moose instead of confess X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8dfdf793de043a5874accdacccfff5dac4f7a7df;p=gitmo%2FMoose.git Use Moose::Util::throw in lib/Moose instead of confess --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 871605a..78bb5b5 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -65,7 +65,7 @@ sub has { my $meta = shift; my $name = shift; - Moose->throw_error('Usage: has \'name\' => ( key => value, ... )') + Moose::Util::throw('Usage: has \'name\' => ( key => value, ... )') if @_ % 2 == 1; my %options = ( definition_context => Moose::Util::_caller_info(), @_ ); @@ -138,15 +138,15 @@ sub init_meta { my %args = @_; my $class = $args{for_class} - or Moose->throw_error("Cannot call init_meta without specifying a for_class"); + or Moose::Util::throw("Cannot call init_meta without specifying a for_class"); my $base_class = $args{base_class} || 'Moose::Object'; my $metaclass = $args{metaclass} || 'Moose::Meta::Class'; my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta'; - Moose->throw_error("The Metaclass $metaclass must be loaded. (Perhaps you forgot to 'use $metaclass'?)") + Moose::Util::throw("The Metaclass $metaclass must be loaded. (Perhaps you forgot to 'use $metaclass'?)") unless is_class_loaded($metaclass); - Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Class.") + Moose::Util::throw("The Metaclass $metaclass must be a subclass of Moose::Meta::Class.") unless $metaclass->isa('Moose::Meta::Class'); # make a subtype for each Moose class @@ -159,9 +159,9 @@ sub init_meta { unless ( $meta->isa("Moose::Meta::Class") ) { my $error_message = "$class already has a metaclass, but it does not inherit $metaclass ($meta)."; if ( $meta->isa('Moose::Meta::Role') ) { - Moose->throw_error($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.'); + Moose::Util::throw($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.'); } else { - Moose->throw_error($error_message); + Moose::Util::throw($error_message); } } } else { diff --git a/lib/Moose/Meta/Attribute/Native/Trait.pm b/lib/Moose/Meta/Attribute/Native/Trait.pm index b006546..c55cc55 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait.pm @@ -5,6 +5,7 @@ use Moose::Role; use Class::Load qw(load_class); use List::MoreUtils qw( any uniq ); use Moose::Util::TypeConstraints; +use Moose::Util; use Moose::Deprecated; requires '_helper_type'; @@ -91,8 +92,7 @@ sub _check_helper_type { $options->{isa} ); ( $isa->is_a_type_of($type) ) - || confess - "The type constraint for $name must be a subtype of $type but it's a $isa"; + || Moose::Util::throw("The type constraint for $name must be a subtype of $type but it's a $isa"); } before 'install_accessors' => sub { (shift)->_check_handles_values }; @@ -108,8 +108,7 @@ sub _check_handles_values { my $accessor_class = $self->_native_accessor_class_for($name); ( $accessor_class && $accessor_class->can('new') ) - || confess - "$name is an unsupported method type - $accessor_class"; + || Moose::Util::throw("$name is an unsupported method type - $accessor_class"); } } diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index b0152ee..2a3f7cf 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -629,9 +629,9 @@ sub _fix_class_metaclass_incompatibility { if ($self->_class_metaclass_can_be_made_compatible($super_meta)) { ($self->is_pristine) - || confess "Can't fix metaclass incompatibility for " + || Moose::Util::throw("Can't fix metaclass incompatibility for " . $self->name - . " because it is not pristine."; + . " because it is not pristine."); my $super_meta_name = $super_meta->_real_ref_name; my $class_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass(blessed($self), $super_meta_name); my $new_self = $class_meta_subclass_meta_name->reinitialize( @@ -650,9 +650,9 @@ sub _fix_single_metaclass_incompatibility { if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) { ($self->is_pristine) - || confess "Can't fix metaclass incompatibility for " + || Moose::Util::throw("Can't fix metaclass incompatibility for " . $self->name - . " because it is not pristine."; + . " because it is not pristine."); my $super_meta_name = $super_meta->_real_ref_name; my $class_specific_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass($self->$metaclass_type, $super_meta->$metaclass_type); my $new_self = $super_meta->reinitialize( diff --git a/lib/Moose/Meta/Method/Accessor/Native.pm b/lib/Moose/Meta/Method/Accessor/Native.pm index f1ff204..54f3cd1 100644 --- a/lib/Moose/Meta/Method/Accessor/Native.pm +++ b/lib/Moose/Meta/Method/Accessor/Native.pm @@ -3,8 +3,8 @@ package Moose::Meta::Method::Accessor::Native; use strict; use warnings; -use Carp qw( confess ); use Scalar::Util qw( blessed weaken ); +use Moose::Util; use Moose::Role; @@ -16,7 +16,7 @@ around new => sub { $options{curried_arguments} = [] unless exists $options{curried_arguments}; - confess 'You must supply a curried_arguments which is an ARRAY reference' + Moose::Util::throw('You must supply a curried_arguments which is an ARRAY reference') unless $options{curried_arguments} && ref($options{curried_arguments}) eq 'ARRAY'; diff --git a/lib/Moose/Meta/Method/Delegation.pm b/lib/Moose/Meta/Method/Delegation.pm index 32436d6..1448ce9 100644 --- a/lib/Moose/Meta/Method/Delegation.pm +++ b/lib/Moose/Meta/Method/Delegation.pm @@ -4,7 +4,7 @@ package Moose::Meta::Method::Delegation; use strict; use warnings; -use Carp 'confess'; +use Moose::Util; use Scalar::Util 'blessed', 'weaken'; use base 'Moose::Meta::Method', @@ -16,20 +16,20 @@ sub new { my %options = @_; ( exists $options{attribute} ) - || confess "You must supply an attribute to construct with"; + || Moose::Util::throw "You must supply an attribute to construct with"; ( blessed( $options{attribute} ) && $options{attribute}->isa('Moose::Meta::Attribute') ) - || confess + || Moose::Util::throw "You must supply an attribute which is a 'Moose::Meta::Attribute' instance"; ( $options{package_name} && $options{name} ) - || confess + || Moose::Util::throw "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} ) || ( 'CODE' eq ref $options{delegate_to_method} ) ) - || confess + || Moose::Util::throw 'You must supply a delegate_to_method which is a method name or a CODE reference'; exists $options{curried_arguments} @@ -37,7 +37,7 @@ sub new { ( $options{curried_arguments} && ( 'ARRAY' eq ref $options{curried_arguments} ) ) - || confess 'You must supply a curried_arguments which is an ARRAY reference'; + || Moose::Util::throw 'You must supply a curried_arguments which is an ARRAY reference'; my $self = $class->_new( \%options ); diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 5ec4f18..491af7c 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -6,7 +6,6 @@ use metaclass; use Class::Load qw(load_class); use Scalar::Util 'blessed'; -use Carp 'confess'; use Devel::GlobalDestruction 'in_global_destruction'; use Moose::Meta::Class; @@ -503,15 +502,15 @@ sub create { my %options = @args; (ref $options{attributes} eq 'HASH') - || confess "You must pass a HASH ref of attributes" + || Moose::Util::throw "You must pass a HASH ref of attributes" if exists $options{attributes}; (ref $options{methods} eq 'HASH') - || confess "You must pass a HASH ref of methods" + || Moose::Util::throw "You must pass a HASH ref of methods" if exists $options{methods}; (ref $options{roles} eq 'ARRAY') - || confess "You must pass an ARRAY ref of roles" + || Moose::Util::throw "You must pass an ARRAY ref of roles" if exists $options{roles}; my $package = delete $options{package}; diff --git a/lib/Moose/Meta/Role/Attribute.pm b/lib/Moose/Meta/Role/Attribute.pm index bc0bd1f..2e629e2 100644 --- a/lib/Moose/Meta/Role/Attribute.pm +++ b/lib/Moose/Meta/Role/Attribute.pm @@ -3,7 +3,6 @@ package Moose::Meta::Role::Attribute; use strict; use warnings; -use Carp 'confess'; use List::MoreUtils 'all'; use Scalar::Util 'blessed', 'weaken'; @@ -48,7 +47,7 @@ sub new { my ( $class, $name, %options ) = @_; (defined $name) - || confess "You must provide a name for the attribute"; + || Moose::Util::throw "You must provide a name for the attribute"; my $role = delete $options{_original_role}; @@ -64,7 +63,7 @@ sub attach_to_role { my ( $self, $role ) = @_; ( blessed($role) && $role->isa('Moose::Meta::Role') ) - || confess + || Moose::Util::throw "You must pass a Moose::Meta::Role instance (or a subclass)"; weaken( $self->{'associated_role'} = $role ); diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 974d59d..c8d76b5 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -10,7 +10,6 @@ use overload '0+' => sub { refaddr(shift) }, # id an object bool => sub { 1 }, fallback => 1; -use Carp qw(confess); use Class::Load qw(load_class); use Eval::Closure; use Scalar::Util qw(blessed refaddr); @@ -132,7 +131,7 @@ sub new { if ( exists $args{message} && (!ref($args{message}) || ref($args{message}) ne 'CODE') ) { - confess("The 'message' parameter must be a coderef"); + Moose::Util::throw("The 'message' parameter must be a coderef"); } my $self = $class->_new(%args); diff --git a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm index aa8cf2d..592bc8d 100644 --- a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm +++ b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm @@ -8,8 +8,6 @@ use base 'Moose::Meta::TypeConstraint'; use Moose::Meta::TypeConstraint::Parameterized; use Moose::Util::TypeConstraints (); -use Carp 'confess'; - __PACKAGE__->meta->add_attribute('constraint_generator' => ( accessor => 'constraint_generator', predicate => 'has_constraint_generator', @@ -50,7 +48,7 @@ sub _can_coerce_constraint_from { sub generate_inline_for { my ($self, $type, $val) = @_; - confess "Can't generate an inline constraint for $type, since none " + Moose::Util::throw "Can't generate an inline constraint for $type, since none " . "was defined" unless $self->has_inline_generator; diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index b44a0eb..066d4b4 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -159,7 +159,7 @@ sub create_class_type_constraint { if (my $type = $REGISTRY->get_type_constraint($class)) { if (!($type->isa('Moose::Meta::TypeConstraint::Class') && $type->class eq $class)) { - _confess( + Moose::Util::throw( "The type constraint '$class' has already been created in " . $type->_package_defined_in . " and cannot be created again in " @@ -195,7 +195,7 @@ sub create_role_type_constraint { if (my $type = $REGISTRY->get_type_constraint($role)) { if (!($type->isa('Moose::Meta::TypeConstraint::Role') && $type->role eq $role)) { - _confess( + Moose::Util::throw( "The type constraint '$role' has already been created in " . $type->_package_defined_in . " and cannot be created again in " @@ -289,13 +289,6 @@ sub normalize_type_constraint_name { return $type_constraint_name; } -sub _confess { - my $error = shift; - - local $Carp::CarpLevel = $Carp::CarpLevel + 1; - Carp::confess($error); -} - ## -------------------------------------------------------- ## exported functions ... ## -------------------------------------------------------- @@ -533,7 +526,7 @@ sub _create_type_constraint ($$$;$$) { my $type = $REGISTRY->get_type_constraint($name); ( $type->_package_defined_in eq $pkg_defined_in ) - || _confess( + || Moose::Util::throw( "The type constraint '$name' has already been created in " . $type->_package_defined_in . " and cannot be created again in "