1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
package Catmandu::Fix::error;
use Catmandu::Sane;
our $VERSION = '1.2024';
use Moo;
use Catmandu::Util qw(is_value);
use Catmandu::Util::Path qw(:all);
use namespace::clean;
use Catmandu::Fix::Has;
with 'Catmandu::Fix::Builder';
has message => (fix_arg => 1);
sub _build_fixer {
my ($self) = @_;
my $msg = $self->message;
if (looks_like_path($msg)) {
my $getter = as_path($msg)->getter;
sub {
my $data = $_[0];
my $vals = $getter->($data);
@$vals || return $data;
my $str = join "\n", grep {is_value($_)} @$vals;
Catmandu::Error->throw($str);
};
}
else {
sub {Catmandu::Error->throw($msg)};
}
}
1;
__END__
=pod
=head1 NAME
Catmandu::Fix::error - die with an error message
=head1 SYNOPSIS
unless exists(id)
error('id missing!')
end
# get the value from a path
error($.error)
=head1 SEE ALSO
L<Catmandu::Fix>
=cut
|