From 1b54c701d663c410c550e40799b6b19112ce5c94 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Thu, 5 Nov 2020 15:49:00 +0000 Subject: [PATCH] Bug 18595: (follow-up) Catch message preference exceptions in OPAC This patch handles message preference exceptions in OPAC messaging template instead of the usual white "Internal Server Error" screen. To test: 0. This test plan assumes you have a default Koha installation that adds message attributes and transports from installer/data/mysql/mandatory/sample_notices_message_attributes.sql and installer/data/mysql/mandatory/sample_notices_message_transports.sql 1. Open developer tools, select console and run the following command: $('#email1').val('non-existing message transport type').prop('checked', true); $('form[name=opacmessaging]').submit(); Observe error: Message transport type non-existing message transport type does not exist. 2. Open developer tools, select console and run the following command: $($('select[name="2-DAYS"] option')[0]).attr('value', 31); $('form[name=opacmessaging]').submit(); Observe error: Days in advance selection is out of range. The value must be between 0 and 30. 3. Run SQL command: delete from message_transports where message_attribute_id=1 and is_digest=0 \ and message_transport_type='email'; Open developer tools, select console and run the following command: $('#digest1').prop('checked', false); $('#forcedigest1').remove(); $('form[name=opacmessaging]').submit(); Observe error: Digest must be selected for Item_Due. Recover your test database by running the following SQL command: insert into message_transports (message_attribute_id, message_transport_type, \ is_digest, letter_module, letter_code) values (1, 'email', 0, 'circulation', \ 'DUE'); 4. Remove your email address, open developer tools, select console and run the following command: $('#email1').removeAttr('disabled'); $('#email1').prop('checked', true); $('form[name=opacmessaging]').submit(); Observe error: You cannot select email notifications because you haven't set an email address. 5. Remove your SMS number, open developer tools, select console and run the following command: $('#sms1').removeAttr('disabled'); $('#sms1').prop('checked', true); $('form[name=opacmessaging]').submit(); Observe error: You cannot select SMS notifications because you haven't set an SMS number address. 6. The rest of the template error messages are currently inaccessible and therefore untestable via GUI, but check the changes this patch introduces manually. These exceptions are: Koha::Exceptions::Patron::Message::Preference::AttributeNotFound Koha::Exceptions::Patron::Message::Preference::DaysInAdvanceNotAvailable Koha::Exceptions::Patron::Message::Preference::DigestNotAvailable You can test them programmatically by searching opac-messaging.pl for the following: C4::Form::MessagingPreferences::handle_form_action( ... and throwing each exception before it by following code: Koha::Exceptions::Patron::Message::Preference::AttributeNotFound->throw(); Sponsored-by: The National Library of Finland --- .../bootstrap/en/modules/opac-messaging.tt | 24 +++++++++++++++++++ opac/opac-messaging.pl | 16 ++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt index f4fabdf9a5..75dc395d1f 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt @@ -34,6 +34,30 @@ [% IF Koha.Preference( 'EnhancedMessagingPreferencesOPAC' ) %]

Your messaging settings

+ [% IF message_preference_exception %] +
+

Error

+ [% IF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Transport::TypeNotFound' %] +

Message transport type [% message_preference_exception.transport_type %] does not exist.

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::AttributeNotFound' %] +

Message attribute not found.

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::DaysInAdvanceOutOfRange' %] +

Days in advance selection is out of range. The value must be between [% message_preference_exception.min %] and [% message_preference_exception.max %].

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::DaysInAdvanceNotAvailable' %] +

Days in advance cannot be selected for [% message_preference_exception.message_name %].

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::DigestNotAvailable' %] +

Digest cannot be selected for [% message_preference_exception.message_name %].

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::DigestRequired' %] +

Digest must be selected for [% message_preference_exception.message_name %].

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::EmailAddressRequired' %] +

You cannot select email notifications because you haven't set an email address.

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::NoTransportType' %] +

Message transport type [% message_preference_exception.transport_type %] is not available for [% message_preference_exception.message_name %].

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::Message::Preference::SMSNumberRequired' %] +

You cannot select SMS notifications because you haven't set an SMS number address.

+ [% END %] +
+ [% END %] [% IF !logged_in_user.email %]

Cannot select email notifications

diff --git a/opac/opac-messaging.pl b/opac/opac-messaging.pl index d468d7ebd0..e564ea3e94 100755 --- a/opac/opac-messaging.pl +++ b/opac/opac-messaging.pl @@ -20,6 +20,8 @@ use Modern::Perl; use CGI qw ( -utf8 ); +use Scalar::Util qw( blessed ); +use Try::Tiny; use C4::Auth; # checkauth, getborrowernumber. use C4::Context; @@ -64,8 +66,20 @@ if ( defined $query->param('modify') && $query->param('modify') eq 'yes' ) { sms_provider_id => $sms_provider_id, })->store; - C4::Form::MessagingPreferences::handle_form_action($query, { borrowernumber => $patron->borrowernumber }, $template); + try { + C4::Form::MessagingPreferences::handle_form_action($query, { borrowernumber => $patron->borrowernumber }, $template); + + } catch { + unless ( blessed $_ && $_->can('rethrow') ) { + die $_; + } else { + $template->param( + message_preference_exception => $_, + message_preference_exception_ref => ref($_), # FIXME ref() does not exist in Template Toolkit, not sure if this is the best approach + ); + } + }; if ( C4::Context->preference('TranslateNotices') ) { $patron->set({ lang => scalar $query->param('lang') })->store; } -- 2.17.1