Bug 7317: Handle missing email addresses gracefuly
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 7 Nov 2017 15:05:04 +0000 (12:05 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 9 Nov 2017 14:42:15 +0000 (11:42 -0300)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Koha/Exceptions/Ill.pm
Koha/Illrequest.pm
ill/ill-requests.pl
koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt

index 9e13761..2084ed0 100644 (file)
@@ -25,6 +25,14 @@ use Exception::Class (
     'Koha::Exceptions::Ill::InvalidBackendId' => {
         isa => 'Koha::Exceptions::Ill',
         description => "Invalid backend name required",
+    },
+    'Koha::Exceptions::Ill::NoTargetEmail' => {
+        isa => 'Koha::Exceptions::Ill',
+        description => "ILL partner library has no email address configured",
+    },
+    'Koha::Exceptions::Ill::NoLibraryEmail' => {
+        isa => 'Koha::Exceptions::Ill',
+        description => "Invalid backend name required",
     }
 );
 
@@ -42,6 +50,14 @@ Generic Ill exception
 
 Exception to be used when the required ILL backend is invalid.
 
+=head2 Koha::Exceptions::Ill::NoTargetEmail
+
+Exception to be used when the ILL partner has no email address set.
+
+=head2 Koha::Exceptions::Ill::NoLibraryEmail
+
+Exception to be used when the current library has no email address set.
+
 =cut
 
 1;
index db5b9a9..53e9ddc 100644 (file)
@@ -892,12 +892,16 @@ EOF
             $to =~ s/^\x00//;       # Strip leading NULLs
             $to =~ s/\x00/; /;      # Replace others with '; '
         }
-        die "No target email addresses found. Either select at least one partner or check your ILL partner library records." if ( !$to );
+        Koha::Exceptions::Ill::NoTargetEmail->throw(
+            "No target email addresses found. Either select at least one partner or check your ILL partner library records.")
+          if ( !$to );
         # Create the from, replyto and sender headers
         my $from = $branch->branchemail;
         my $replyto = $branch->branchreplyto || $from;
-        die "Your branch has no email address. Please set it."
-            if ( !$from );
+        Koha::Exceptions::Ill::NoLibraryEmail->throw(
+            "Your library has no usable email address. Please set it.")
+          if ( !$from );
+
         # Create the email
         my $message = Koha::Email->new;
         my %mail = $message->create_message_headers(
index 140eff9..6d37512 100755 (executable)
@@ -27,6 +27,8 @@ use Koha::AuthorisedValues;
 use Koha::Illrequests;
 use Koha::Libraries;
 
+use Try::Tiny;
+
 our $cgi = CGI->new;
 my $illRequests = Koha::Illrequests->new;
 
@@ -182,17 +184,38 @@ if ( $backends_available ) {
         handle_commit_maybe($backend_result, $request);
 
     } elsif ( $op eq 'generic_confirm' ) {
-        my $request = Koha::Illrequests->find($params->{illrequest_id});
-        $params->{current_branchcode} = C4::Context->mybranch;
-        my $backend_result = $request->generic_confirm($params);
-        $template->param(
-            whole => $backend_result,
-            request => $request,
-        );
-
-        # handle special commit rules & update type
-        handle_commit_maybe($backend_result, $request);
+        try {
+            my $request = Koha::Illrequests->find($params->{illrequest_id});
+            $params->{current_branchcode} = C4::Context->mybranch;
+            my $backend_result = $request->generic_confirm($params);
+            $template->param(
+                whole => $backend_result,
+                request => $request,
+            );
+            $template->param( error => $params->{error} )
+                if $params->{error};
 
+            # handle special commit rules & update type
+            handle_commit_maybe($backend_result, $request);
+        }
+        catch {
+            my $error;
+            if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
+                $error = 'no_target_email';
+            }
+            elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
+                $error = 'no_library_email';
+            }
+            else {
+                $error = 'unknown_error';
+            }
+            print $cgi->redirect(
+                "/cgi-bin/koha/ill/ill-requests.pl?" .
+                "method=generic_confirm&illrequest_id=" .
+                $params->{illrequest_id} .
+                "&error=$error" );
+            exit;
+        };
     } elsif ( $op eq 'illlist') {
         # Display all current ILLs
         my $requests = $illRequests->search();
index e57115b..0c41d02 100644 (file)
 
                 [% ELSIF query_type == 'generic_confirm' %]
                     <h1>Place request with partner libraries</h1>
+                  [% IF error %]
+                      <div class="alert">
+                    [% IF error == 'no_target_email' %]
+                          No target email addresses found. Either select at least
+                          one partner or check your ILL partner library records.
+                    [% ELSIF error == 'no_library_email' %]
+                          Your library has no usable email address. Please set it.
+                    [% ELSIF error == 'unkown_error' %]
+                          Unknown error processing your request. Contact your administrator.
+                    [% END %]
+                      </div>
+                  [% END %]
                     <!-- Start of GENERIC_EMAIL case -->
                     [% IF whole.value.partners %]
                        [% ill_url = here_link _ "?method=illview&illrequest_id=" _ request.illrequest_id %]