From 82155cdf35fa215e3d7bc600951f7594e4d166c3 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 4 Oct 2021 12:08:12 -0300 Subject: [PATCH] Bug 27945: Fix error handling and translatability This patch adds better error handling and reporting when placing an article request fails. It also makes the error messages translatable. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart --- circ/request-article.pl | 21 +++++++++--- .../prog/en/modules/circ/request-article.tt | 5 ++- .../en/modules/opac-request-article.tt | 5 ++- opac/opac-request-article.pl | 32 ++++++++++++++----- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/circ/request-article.pl b/circ/request-article.pl index 0cc4cc591d..1c92927b82 100755 --- a/circ/request-article.pl +++ b/circ/request-article.pl @@ -25,8 +25,11 @@ use C4::Utils::DataTables::Members; use C4::Search qw( enabled_staff_search_views ); use C4::Serials qw( CountSubscriptionFromBiblionumber ); use Koha::Biblios; +use Koha::Logger; use Koha::Patrons; use Koha::ArticleRequests; + +use Scalar::Util qw( blessed ); use Try::Tiny; my $cgi = CGI->new; @@ -86,11 +89,19 @@ if ( $action eq 'create' ) { patron_notes => $patron_notes, format => $format, } - )->store(); + )->request; } catch { - $template->param( - error_message => $_->{message} - ); + if ( blessed $_ and $_->isa('Koha::Exceptions::ArticleRequest::LimitReached') ) { + $template->param( + error_message => 'article_request_limit_reached' + ); + } + else { + Koha::Logger->get->debug("Unhandled exception when placing an article request ($_)"); + $template->param( + error_message => 'article_request_unhandled_exception' + ); + } }; } @@ -119,7 +130,7 @@ if ( !$patron && $patron_cardnumber ) { if( $patron && !$patron->can_request_article) { $patron = undef; $template->param( - error_message => 'Patron cannot request more articles for today' + error_message => 'article_request_limit_reached' ); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt index f26a10318d..d1c3306b89 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt @@ -48,7 +48,10 @@

Request article from [% INCLUDE 'biblio-title.inc' link = 1 %]

[% IF error_message %]
-

[% error_message | html %]

+ [% SWITCH error_message %] + [% CASE 'article_request_limit_reached' %]

Patron reached daily limit.

+ [% CASE 'article_request_unhandled_exception' %]

An error has occurred. Check the logs.

+ [% END %]
[% END %] [% IF no_patrons_found %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt index e504556b74..9dad25f038 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt @@ -219,7 +219,10 @@ [% ELSIF error_message %]

[% biblio.title | html %]

- [% error_message | html %] + [% SWITCH error_message %] + [% CASE 'article_request_limit_reached' %]You reached your article requests daily limit. + [% CASE 'article_request_unhandled_exception' %]An error has occurred. + [% END %]
[% ELSE %]

[% biblio.title | html %]

diff --git a/opac/opac-request-article.pl b/opac/opac-request-article.pl index 146454f033..b6048cc8c2 100755 --- a/opac/opac-request-article.pl +++ b/opac/opac-request-article.pl @@ -25,7 +25,10 @@ use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); use Koha::Biblios; +use Koha::Logger; use Koha::Patrons; + +use Scalar::Util qw( blessed ); use Try::Tiny; my $cgi = CGI->new; @@ -60,6 +63,9 @@ if ( $action eq 'create' ) { my $patron_notes = $cgi->param('patron_notes') || undef; my $format = $cgi->param('format') || undef; + + my $success; + try { my $ar = Koha::ArticleRequest->new( { @@ -77,16 +83,26 @@ if ( $action eq 'create' ) { patron_notes => $patron_notes, format => $format, } - )->store(); + )->request; + $success = 1; + } catch { + if ( blessed $_ and $_->isa('Koha::Exceptions::ArticleRequest::LimitReached') ) { + $template->param( + error_message => 'article_request_limit_reached' + ); + } + else { + Koha::Logger->get->debug("Unhandled exception when placing an article request ($_)"); + $template->param( + error_message => 'article_request_unhandled_exception' + ); + } + }; + if ( $success ) { print $cgi->redirect("/cgi-bin/koha/opac-user.pl#opac-user-article-requests"); exit; - } catch { - exit unless $_->[0] && $_->[0] eq 'EXIT'; - $template->param( - error_message => $_->{message} - ); - }; + } # Should we redirect? } elsif ( !$action && C4::Context->preference('ArticleRequestsOpacHostRedirection') ) { @@ -106,7 +122,7 @@ my $patron = Koha::Patrons->find($borrowernumber); if(!$patron->can_request_article) { $template->param( - error_message => 'You cannot request more articles for today' + error_message => 'article_request_limit_reached' ); } -- 2.39.5