Bug 20950

Summary: Plugins: Handler.pm doesn't fail gracefully when running plugin
Product: Koha Reporter: Andrew Isherwood <bugzilla>
Component: Plugin architectureAssignee: Bugs List <koha-bugs>
Status: NEW --- QA Contact: Testopia <testopia>
Severity: normal    
Priority: P5 - low    
Version: Main   
Hardware: All   
OS: All   
GIT URL: Change sponsored?: ---
Patch complexity: --- Documentation contact:
Documentation submission: Text to go in the release notes:
Version(s) released in:
Circulation function:

Description Andrew Isherwood 2018-06-15 10:55:56 UTC
When running a plugin that fails for some internal reason, the user doesn't always receive a very helpful message. With the Kitchen Sink plugin ( https://github.com/bywatersolutions/koha-plugin-kitchen-sink ) I managed to break it by trying to import C4::Members::GetMember - which doesn't exist any more.

Running the plugin in this broken state resulted in:

run.pl: Plugin Koha::Plugin::Com::ByWaterSolutions::KitchenSink cannot be loaded at /usr/share/koha/Koha/Plugins/Handler.pm line 72.: /usr/share/koha/plugins/run.pl

It turns out that Koha::Plugins::Handler::run calls Module::Load::Conditional::can_load, which was returning false, which caused the message I saw to be output. However, Module::Load::Conditional sets a global variable upon can_load returning false containing the reason. We should display this (assuming we don't consider it to be a security risk).

So, in Handler.pm, we'd need to substitute in:

if ( can_load( modules => { $plugin_class => undef } ) ) {
    my $plugin = $plugin_class->new( { cgi => $cgi, enable_plugins => $args->{'enable_plugins'} } );
    if ( $plugin->can($plugin_method) ) {
        return $plugin->$plugin_method( $params );
    } else {
        warn "Plugin does not have method $plugin_method";
    }
} else {
    warn "Plugin $plugin_class cannot be loaded: " . $Module::Load::Conditional::ERROR;
}