From 62d3e3a8e4f4183af67c0ebc6be4f0f3da77c1fa Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Thu, 9 May 2024 03:04:34 +0200 Subject: [PATCH] Improve error messages Commit 52749df5 added more information to error messages to aid debugging, but most plugin follow-up questions are code references, not method names, and they would result in an ugly CODE(0x...) in the error message. This change adds the fully qualified name of plugin methods. Not sure if I like that, I might drop the RevBank::Plugin:: prefix at some point. --- revbank | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/revbank b/revbank index 7238c08..5d20f0c 100755 --- a/revbank +++ b/revbank @@ -6,6 +6,7 @@ use experimental 'isa'; # stable since v5.36 use experimental 'signatures'; # stable since v5.36 use List::Util qw(uniq); +use Sub::Util qw(subname); use POSIX qw(ttyname); use FindBin qw($RealBin); @@ -167,6 +168,11 @@ OUTER: for (;;) { "unexpected trailing input (use ';' to separate transactions)." ); + my $coderef = ref($method) ? $method : $plugin->can($method); + my ($mname) = $coderef + ? (subname($coderef) eq "__ANON__" ? "" : subname($coderef) . ": ") + : (ref($method) ? "" : "$method: "); + my ($rv, @rvargs) = ($word =~ /[^\x20-\x7f]/ and $method eq 'command' || !$plugin->AllChars($method)) @@ -183,11 +189,12 @@ OUTER: for (;;) { $retry = $@->reason; redo OUTER; } elsif ($@) { - call_hooks "plugin_fail", $plugin->id, "$method: $@"; + call_hooks "plugin_fail", $plugin->id, "$mname$@"; abort; } + if (not defined $rv) { - call_hooks "plugin_fail", $plugin->id, "$method: No return code"; + call_hooks "plugin_fail", $plugin->id, $mname . "No return code"; abort; } if (not ref $rv) { @@ -204,8 +211,10 @@ OUTER: for (;;) { $prompt = $rv; @plugins = $plugin; ($method) = @rvargs; - call_hooks "plugin_fail", $plugin->id, "$method: No method supplied" - if not ref $method; + if (not ref $method) { + call_hooks "plugin_fail", $plugin->id, $mname . "No method supplied"; + abort; + } next WORD; } @@ -247,11 +256,11 @@ OUTER: for (;;) { } if ($rv == NEXT) { next PLUGIN if $method eq 'command'; - call_hooks "plugin_fail", $plugin->id, "$method: " + call_hooks "plugin_fail", $plugin->id, $mname . "Only 'command' should ever return NEXT."; abort; } - call_hooks "plugin_fail", $plugin->id, "$method: Invalid return value"; + call_hooks "plugin_fail", $plugin->id, $mname . "Invalid return value"; abort; } call_hooks "invalid_input", $cart, $origword, $word, \@allwords;