Skip to content

Commit

Permalink
CAF::Path: fix _safe_eval()
Browse files Browse the repository at this point in the history
- Properly detect an error when the evaluated function produces an error
  • Loading branch information
jouvin committed Mar 7, 2017
1 parent b84520a commit 49e3b2c
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/perl/Path.pm
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ sub _function_catch
Run function reference C<funcref> with arrayref C<argsref> and hashref C<optsref>.
Return and set fail attribute with C<failmsg> on die, verbose C<msg> on success
(resp. $@ and stringified result are appended).
Return and set fail attribute with C<failmsg> on die or an error (C<undef> returned
by C<funcref>), or print (at verbose level) C<msg> on success (respectively $@ and
stringified result are appended). Note that C<_safe_eval> doesn't work with functions
that don't return a defined value when they succeed.
Resets previous exceptions and/or fail attribute
Expand All @@ -214,17 +216,21 @@ sub _safe_eval
%opts = %$optsref if $optsref;

local $@;
eval {
$res = $funcref->(@args, %opts);
};
my $res = eval {
$funcref->(@args, %opts);
};

if ($@) {
chomp($@);
return $self->fail("$failmsg: $@");
} else {
my $res_txt = defined($res) ? "$res" : '<undef>';
chomp($res);
# $res is undef if there is a syntax or runtime error or if the evaluated
# function returns undef (interpreted as a function error).
if ( defined($res) ) {
$self->verbose("$msg: $res");
} else {
my $err_msg = '';
if ($@) {
chomp($@);
$err_msg = ": $@";
}
return $self->fail("$failmsg$err_msg");
}

return $res;
Expand Down

0 comments on commit 49e3b2c

Please sign in to comment.