MooseX::Constructor::AllErrors::Error::Constructor - error class for MooseX::Constructor::AllErrors


MooseX-Constructor-AllErrors documentation Contained in the MooseX-Constructor-AllErrors distribution.

Index


Code Index:

NAME

Top

MooseX::Constructor::AllErrors::Error::Constructor - error class for MooseX::Constructor::AllErrors

VERSION

Top

version 0.016

DESCRIPTION

Top

$@ will contain an instance of this class when MooseX::Constructor::AllErrors throws an exception during object construction.

METHODS

Top

errors

Returns a list of MooseX::Constructor::AllErrors::Error objects representing each error that was found.

missing

Returns a list of MooseX::Constructor::AllErrors::Error::Required objects representing each missing argument error that was found.

invalid

Returns a list of MooseX::Constructor::AllErrors::Error::TypeConstraint objects representing each type constraint error that was found.

SEE ALSO

Top

Moose

AUTHOR

Top

  Hans Dieter Pearcey <hdp@cpan.org>

COPYRIGHT AND LICENSE

Top


MooseX-Constructor-AllErrors documentation Contained in the MooseX-Constructor-AllErrors distribution.

package MooseX::Constructor::AllErrors::Error::Constructor;
BEGIN {
  $MooseX::Constructor::AllErrors::Error::Constructor::VERSION = '0.016';
}

use Moose;

has errors => (
    is => 'ro',
    isa => 'ArrayRef[MooseX::Constructor::AllErrors::Error]',
    auto_deref => 1,
    lazy => 1,
    default => sub { [] },
);

has caller => (
    is => 'ro',
    isa => 'ArrayRef',
    required => 1,
);

sub _errors_by_type {
    my ($self, $type) = @_;
    return [ grep { 
        $_->isa("MooseX::Constructor::AllErrors::Error::$type")
    } $self->errors ];
}

has missing => (
    is => 'ro',
    isa => 'ArrayRef[MooseX::Constructor::AllErrors::Error::Required]',
    auto_deref => 1,
    lazy => 1,
    default => sub { shift->_errors_by_type('Required') },
);

has invalid => (
    is => 'ro',
    isa => 'ArrayRef[MooseX::Constructor::AllErrors::Error::TypeConstraint]',
    auto_deref => 1,
    lazy => 1,
    default => sub { shift->_errors_by_type('TypeConstraint') },
);

sub has_errors {
    return scalar @{ $_[0]->errors };
}

sub add_error {
    my ($self, $error) = @_;
    push @{$self->errors}, $error;
}

sub message {
    my $self = shift;
    confess "$self->message called without any errors"
        unless $self->has_errors;
    return $self->errors->[0]->message;
}

sub stringify {
    my $self = shift;
    return '' unless $self->has_errors;
    return sprintf '%s at %s line %d',
        $self->message,
        $self->caller->[1], $self->caller->[2];
}

use overload (
    q{""} => 'stringify',
    fallback => 1,
);

1;
__END__