
A space had a custom plugin that died during hook_checkout, which caused the CHECKOUT lines to be logged without the corresponding BALANCE, and indeed no account balances were updated. While the plugin had a bug, it should not cause a half transaction in RevBank. After some hesitation, I went with ON ERROR RESUME NEXT because if a hook throws an exception, that should not interfere with other plugins (the hook can return ABORT if this it was intentional), including the calling plugin. An error message is printed (but not logged... TODO: add hook_plugin_fail to plugins/log) but the show must go on. During hook_checkout_prepare, however, nothing is set in stone yet, so this could be used for something that might die, and this instance of call_hooks() is now the one place where a failing hook should result in the transaction getting aborted. For this, call_hooks() now returns a success status boolean. Maybe it would make sense in more places, but I didn't identify any such calls yet. RevBank::Cart->checkout used to return a success status boolean, but it could just as well just die (indirectly, to abort the transaction) since it can't be called a second time within the same transaction anyway (because ->set_user must be called exactly once), so continuing with the same transaction can't result in anything useful anyway. In some places, error messages were slightly improved to contain a bit more information.
104 lines
2.6 KiB
Perl
104 lines
2.6 KiB
Perl
package RevBank::Plugins;
|
|
|
|
use v5.28;
|
|
use warnings;
|
|
use experimental 'signatures'; # stable since v5.36
|
|
|
|
use RevBank::Eval;
|
|
use RevBank::Plugin;
|
|
use RevBank::Global;
|
|
use Exporter;
|
|
our @EXPORT = qw(call_hooks load_plugins);
|
|
|
|
my @plugins;
|
|
|
|
sub _read_file($fn) {
|
|
local @ARGV = ($fn);
|
|
readline *ARGV;
|
|
}
|
|
|
|
sub call_hooks {
|
|
my $hook = shift;
|
|
my $method = "hook_$hook";
|
|
my $success = 1;
|
|
|
|
for my $class (@plugins) {
|
|
if ($class->can($method)) {
|
|
my ($rv, @message) = eval { $class->$method(@_) };
|
|
|
|
if ($@) {
|
|
$success = 0;
|
|
call_hooks("plugin_fail", $class->id, "$class->$method died: $@");
|
|
} elsif (defined $rv and ref $rv) {
|
|
main::abort(@message) if $rv == ABORT;
|
|
|
|
$success = 0;
|
|
call_hooks("plugin_fail", $class->id, "$class->$method returned an unsupported value");
|
|
}
|
|
}
|
|
}
|
|
|
|
return $success;
|
|
};
|
|
|
|
sub register(@new_plugins) {
|
|
call_hooks("register", $_) for @new_plugins;
|
|
push @plugins, @new_plugins;
|
|
}
|
|
|
|
sub load($class) {
|
|
my @config = _read_file('revbank.plugins');
|
|
chomp @config;
|
|
s/#.*//g for @config;
|
|
@config = map /(\S+)/, grep /\S/, @config;
|
|
|
|
for my $name (@config) {
|
|
my $fn = "plugins/$name";
|
|
my $package = "RevBank::Plugin::$name";
|
|
if (not -e $fn) {
|
|
warn "$fn does not exist; skipping plugin.\n";
|
|
next;
|
|
}
|
|
RevBank::Eval::clean_eval(qq[
|
|
use strict;
|
|
use warnings;
|
|
use feature qw(signatures state);
|
|
no warnings 'experimental::signatures';
|
|
package $package;
|
|
BEGIN { RevBank::Global->import; }
|
|
our \@ISA = qw(RevBank::Plugin);
|
|
our \%ATTR;
|
|
sub MODIFY_CODE_ATTRIBUTES(\$class, \$sub, \@attrs) {
|
|
\$ATTR{ \$sub } = "\@attrs";
|
|
return;
|
|
}
|
|
sub FETCH_CODE_ATTRIBUTES {
|
|
return \$ATTR{ +pop };
|
|
}
|
|
sub HELP1 {
|
|
\$::HELP1{ +shift } = +pop;
|
|
}
|
|
sub HELP {
|
|
\$::HELP{ +shift } = +pop;
|
|
}
|
|
sub id { '$name' }
|
|
] . "\n#line 1 $fn\n" . join "", _read_file($fn));
|
|
|
|
if ($@) {
|
|
call_hooks("plugin_fail", $name, "Compile error: $@");
|
|
next;
|
|
}
|
|
if (not $package->can("command")) {
|
|
warn "Plugin $name does not have a 'command' method; skipping.\n";
|
|
next;
|
|
}
|
|
|
|
register $package;
|
|
}
|
|
}
|
|
|
|
sub new($class) {
|
|
return map $_->new, @plugins;
|
|
}
|
|
|
|
1;
|