From e6dfa5fc6d999fa44217f5470c2d60c174ad0517 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Mon, 27 Sep 2021 08:18:33 -0300
Subject: [PATCH] Bug 29121: Add new plugin exceptions

This patch introduces two new exceptions:

- Koha::Exceptions::Plugin::InstallDied
- Koha::Exceptions::Plugin::UpgradeDied

Tests are added for their stringification output.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/Koha/Exceptions.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 Koha/Exceptions/Plugin.pm | 42 ++++++++++++++++++++++++++++++++++++++-
 t/Koha/Exceptions.t       | 29 ++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/Koha/Exceptions/Plugin.pm b/Koha/Exceptions/Plugin.pm
index fa3def3cb10..b10525e7fed 100644
--- a/Koha/Exceptions/Plugin.pm
+++ b/Koha/Exceptions/Plugin.pm
@@ -31,7 +31,17 @@ use Exception::Class (
         isa         => 'Koha::Exceptions::Plugin',
         description => 'Required method is missing',
         fields      =>  ['plugin_name','method']
-    }
+    },
+    'Koha::Exceptions::Plugin::InstallDied' => {
+        isa         => 'Koha::Exceptions::Plugin',
+        description => 'The plugin died on install',
+        fields      => ['plugin_class'],
+    },
+    'Koha::Exceptions::Plugin::UpgradeDied' => {
+        isa         => 'Koha::Exceptions::Plugin',
+        description => 'The plugin died on upgrade',
+        fields      => ['plugin_class'],
+    },
 );
 
 sub full_message {
@@ -43,6 +53,12 @@ sub full_message {
         if ( $self->isa('Koha::Exceptions::Plugin::MissingMethod') ) {
             $msg = sprintf("Cannot use plugin (%s) because the it doesn't implement the '%s' method which is required.", $self->plugin_name, $self->method );
         }
+        elsif ( $self->isa('Koha::Exceptions::Plugin::InstallDied') ) {
+            $msg = sprintf("Calling 'install' died for plugin %s", $self->plugin_class);
+        }
+        elsif ( $self->isa('Koha::Exceptions::Plugin::UpgradeDied') ) {
+            $msg = sprintf("Calling 'upgrade' died for plugin %s", $self->plugin_class);
+        }
     }
 
     return $msg;
@@ -73,6 +89,30 @@ method and it doesn't.
 
 =back
 
+=head2 Koha::Exceptions::Plugin::InstallDied
+
+Exception to be used when a plugin 'install' method explodes.
+
+=head3 Parameters
+
+=over
+
+=item plugin_class: the plugin class
+
+=back
+
+=head2 Koha::Exceptions::Plugin::UpgradeDied
+
+Exception to be used when a plugin 'upgrade' method explodes.
+
+=head3 Parameters
+
+=over
+
+=item plugin_class: the plugin class
+
+=back
+
 =head1 Class methods
 
 =head2 full_message
diff --git a/t/Koha/Exceptions.t b/t/Koha/Exceptions.t
index 3ffb34b2995..56107fccc46 100755
--- a/t/Koha/Exceptions.t
+++ b/t/Koha/Exceptions.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 8;
+use Test::More tests => 9;
 use Test::MockObject;
 use Test::Exception;
 
@@ -309,3 +309,30 @@ subtest 'Koha::Exceptions::Patron tests' => sub {
         'Exception is thrown :-D';
     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
 };
+
+subtest 'Koha::Exceptions::Plugin tests' => sub {
+
+    plan tests => 5;
+
+    use_ok("Koha::Exceptions::Plugin");
+
+    my $plugin_class = 'yahey';
+
+    throws_ok
+        { Koha::Exceptions::Plugin::InstallDied->throw(
+            plugin_class => $plugin_class ); }
+        'Koha::Exceptions::Plugin::InstallDied',
+        'Exception is thrown :-D';
+
+    # stringify the exception
+    is( "$@", "Calling 'install' died for plugin $plugin_class", 'Exception stringified correctly' );
+
+    throws_ok
+        { Koha::Exceptions::Plugin::UpgradeDied->throw(
+            plugin_class => $plugin_class ); }
+        'Koha::Exceptions::Plugin::UpgradeDied',
+        'Exception is thrown :-D';
+
+    # stringify the exception
+    is( "$@", "Calling 'upgrade' died for plugin $plugin_class", 'Exception stringified correctly' );
+};
-- 
2.20.1