catch errors in report templates
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 20 Nov 2022 22:49:16 +0000 (23:49 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 22 Nov 2022 23:06:47 +0000 (00:06 +0100)
barcat

diff --git a/barcat b/barcat
index 189c8ff72b3688ce8fcd8984313d30503da199d4..6dbddd97910941e84542173cfff807318eaa906a 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -384,10 +384,19 @@ sub show_stat {
 sub varfmt {
        my ($fmt, $vars) = @_;
        $fmt =~ s[\$\{ (\w+) (?<cmd>; (?: [^{}]+ | \{.*?\} )*)? \}]{
-               local $_ = $vars->{$1}; #TODO //
-               $_ = $opt{'value-format'}->($_) if $opt{reformat};
-               eval $+{cmd} if $+{cmd}; #TODO $@
-               $_;
+               local $_ = $vars->{$1};
+               if (defined) {
+                       $_ = $opt{'value-format'}->($_) if $opt{reformat};
+                       if ($+{cmd}) {
+                               eval $+{cmd};
+                               warn "Error in \$$1 report: $@" if $@;
+                       }
+                       $_;
+               }
+               else {
+                       warn "Unknown variable \$$1 in report\n";
+                       "\$$1";
+               }
        }eg;
        return $fmt;
 }