From 459093dba9dd42068311d430fee2900bd41d4052 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Thu, 9 May 2024 03:09:27 +0200 Subject: [PATCH] v6.2.0: Use reject/retry instead of exception for bad amount Since the first versions of RevBank, negative and huge amounts are handled centrally, and since v2 (2013) they've been implemented through an exception that caused the pending transaction to be aborted. Since v3 (2019), RevBank has had a retry mechanism for rejected input to improve the user experience, but it required a REJECT return message from a plugin, not an exception. Now there's an exception class to trigger the same semantics. --- lib/RevBank/Global.pm | 14 ++++++++++++-- revbank | 5 ++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/RevBank/Global.pm b/lib/RevBank/Global.pm index 7e0091b..c5240e6 100644 --- a/lib/RevBank/Global.pm +++ b/lib/RevBank/Global.pm @@ -8,6 +8,12 @@ use POSIX qw(strftime); use RevBank::Amount; use RevBank::FileIO; +{ + package RevBank::Exception::RejectInput; + sub new($class, $reason) { return bless \$reason, $class; } + sub reason($self) { return $$self; } +} + sub import { require RevBank::Plugins; require RevBank::Users; @@ -48,10 +54,14 @@ sub import { $posneg and return undef; # last token must be term if ($amount->cents < 0) { - die "For our sanity, no negative amounts, please :).\n"; + die RevBank::Exception::RejectInput->new( + "For our sanity, no negative amounts, please :)." + ); } if ($amount->cents > 99900) { - die "That's way too much money.\n"; + die RevBank::Exception::RejectInput->new( + "That's way too much money." + ); } return $amount; }; diff --git a/revbank b/revbank index 5d20f0c..aad6e29 100755 --- a/revbank +++ b/revbank @@ -17,7 +17,7 @@ use RevBank::Messages; use RevBank::Cart; use RevBank::Prompt; -our $VERSION = "6.1.5"; +our $VERSION = "6.2.0"; our %HELP1 = ( "abort" => "Abort the current transaction", ); @@ -188,6 +188,9 @@ OUTER: for (;;) { @words = (); $retry = $@->reason; redo OUTER; + } elsif ($@ isa 'RevBank::Exception::RejectInput') { + $rv = REJECT; + @rvargs = $@->reason; } elsif ($@) { call_hooks "plugin_fail", $plugin->id, "$mname$@"; abort;