Skip to content

Commit

Permalink
v6.2.0: Use reject/retry instead of exception for bad amount
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Juerd committed May 9, 2024
1 parent 62d3e3a commit 459093d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 12 additions & 2 deletions lib/RevBank/Global.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down
5 changes: 4 additions & 1 deletion revbank
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 459093d

Please sign in to comment.