From 49e3b2c0521e864391120990e982748396980a17 Mon Sep 17 00:00:00 2001 From: Michel Jouvin Date: Mon, 6 Mar 2017 12:15:42 +0100 Subject: [PATCH] CAF::Path: fix _safe_eval() - Properly detect an error when the evaluated function produces an error --- src/main/perl/Path.pm | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/perl/Path.pm b/src/main/perl/Path.pm index c4641ef4..d3e32f53 100644 --- a/src/main/perl/Path.pm +++ b/src/main/perl/Path.pm @@ -196,8 +196,10 @@ sub _function_catch Run function reference C with arrayref C and hashref C. -Return and set fail attribute with C on die, verbose C on success -(resp. $@ and stringified result are appended). +Return and set fail attribute with C on die or an error (C returned +by C), or print (at verbose level) C 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 @@ -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" : ''; - 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;