From be871ce8f0e249af16c02dbbb4ad5a63b68a235c 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::MessagePreference::AttributeNotFound Koha::Exceptions::Patron::MessagePreference::DaysInAdvanceNotAvailable Koha::Exceptions::Patron::MessagePreference::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::MessagePreference::AttributeNotFound->throw(); Sponsored-by: The National Library of Finland --- .../bootstrap/en/modules/opac-messaging.tt | 24 +++++++++++++++++++ opac/opac-messaging.pl | 18 +++++++++++++- 2 files changed, 41 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 52388be3fc..8718418f4a 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt @@ -37,6 +37,30 @@ [% IF Koha.Preference( 'EnhancedMessagingPreferencesOPAC' ) || Koha.Preference('TranslateNotices') %]

Your messaging settings

+ [% IF message_preference_exception %] +
+

Error

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

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

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

Message attribute not found.

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::MessagePreference::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::MessagePreference::DaysInAdvanceNotAvailable' %] +

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

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

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

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

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

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

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

+ [% ELSIF message_preference_exception_ref == 'Koha::Exceptions::Patron::MessagePreference::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::MessagePreference::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 4e64a4c8d1..89bfcaad95 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 qw( get_template_and_user ); use C4::Context; @@ -62,7 +64,21 @@ 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') ) { -- 2.25.1