From 866cc88111fc30649bdf54dcd63d39a7608774ce Mon Sep 17 00:00:00 2001 From: Robert Lange <robert.lange@uni-leipzig.de> Date: Fri, 29 May 2020 18:28:18 +0200 Subject: [PATCH] refs #15664 [master] set specific error messages for timeouts * add finc bootstrapper with error listener * add finc error templates for timeout and runtime * add translations for timeout and service unavailable * name Templates like Exception class name * fix error message: use system email * use vufind5 coding styles --- local/languages/de.ini | 2 + local/languages/en.ini | 2 + module/finc/Module.php | 3 +- module/finc/src/finc/Bootstrapper.php | 101 ++++++++++++++++++ .../templates/error/RuntimeException.phtml | 56 ++++++++++ .../templates/error/TimeoutException.phtml | 48 +++++++++ 6 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 module/finc/src/finc/Bootstrapper.php create mode 100644 themes/finc/templates/error/RuntimeException.phtml create mode 100644 themes/finc/templates/error/TimeoutException.phtml diff --git a/local/languages/de.ini b/local/languages/de.ini index 336804999db..a0df05da337 100644 --- a/local/languages/de.ini +++ b/local/languages/de.ini @@ -710,6 +710,8 @@ email_selected_favorites = "Ausgewählte Favoriten per E-Mail versenden" email_sending = "Nachricht wird gesendet..." email_success = "Nachricht versendet" error_inconsistent_parameters = "Leider ist ein Fehler aufgetreten. Es wurden widersprüchliche Angaben entdeckt." +error_timeout = "Ihre Abfrage dauerte zu lange und wurde abgebrochen. Bitte versuchen Sie, die Seite neu zu laden oder stellen Sie eine einfachere Abfrage. Falls Ihre Abfrage bereits einfach war, schicken Sie uns bitte eine Nachricht mit der Abfrage (und ggf. URL) an <a href="%s">%s</a>, damit wir das Problem lösen können." +error_service_unavailable = "Die Suche ist vorübergehend nicht verfügbar. Bitte versuchen Sie es später noch einmal." errorcode_error = "Es ist ein Fehler aufgetreten" errorcode_http_status_error = "Kein Netzwerk vorhanden" errorcode_empty_response_error = "Keine Nachricht vom Lokalsystem erhalten" diff --git a/local/languages/en.ini b/local/languages/en.ini index d8e6fdb85ca..a2e3967518e 100644 --- a/local/languages/en.ini +++ b/local/languages/en.ini @@ -698,6 +698,8 @@ email_selected_favorites = "E-mail Selected Favorites" email_sending = "Sending Message..." email_success = "Message Sent" error_inconsistent_parameters = "Sorry, an error has occurred. Inconsistent parameters detected" +error_timeout = "Your query was taking too long and has been canceled. Please try to reload the page or make a simpler query. If your query was already simple, please send us a message with the query (if possible add URL) to <a href="mailto:%s">%s</a>, so that we can solve the problem." +error_service_unavailable = "Sorry, Service Is Temporarily Unavailable. Please retry again later." errorcode_error = "Sorry, an error has occurred. Please try it again" errorcode_http_status_error = "No network available for the Integrated Library System" errorcode_empty_response_error = "No response received from the Integrated Library System" diff --git a/module/finc/Module.php b/module/finc/Module.php index 9472cd3c7d1..f27fae056f9 100644 --- a/module/finc/Module.php +++ b/module/finc/Module.php @@ -85,9 +85,10 @@ class Module * @param MvcEvent $e Event * * @return void - * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function onBootstrap(MvcEvent $e) { + $bootstrapper = new Bootstrapper($e); + $bootstrapper->bootstrap(); } } diff --git a/module/finc/src/finc/Bootstrapper.php b/module/finc/src/finc/Bootstrapper.php new file mode 100644 index 00000000000..fa81aac9a1b --- /dev/null +++ b/module/finc/src/finc/Bootstrapper.php @@ -0,0 +1,101 @@ +<?php +/** + * VuFind Bootstrapper + * + * PHP version 7 + * + * Copyright (C) Leipzig University Library 2020. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Bootstrap + * @author Robert Lange <lange@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Site + */ +namespace finc; + +use Zend\Console\Console; + +/** + * Finc Bootstrapper + * + * @category VuFind + * @package Bootstrap + * @author Robert Lange <lange@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Site + */ +class Bootstrapper extends \VuFind\Bootstrapper +{ + /** + * Bootstrap finc resources. + * + * @return void + */ + public function bootstrap() + { + // automatically call all methods starting with "init": + $methods = get_class_methods($this); + foreach ($methods as $method) { + if (substr($method, 0, 8) == 'initFinc') { + $this->$method(); + } + } + } + + /** + * Set up custom error page based on exception information. + * + * @return void + */ + protected function initFincCustomErrorView() + { + //custom error page not needed in console mode: + if (\Zend\Console\Console::isConsole()) { + return; + } + + $callback = function ($e) { + $exception = $e->getParam('exception'); + if (!$exception) { + return; + } + + $exceptionName = end( + explode('\\', get_class($e->getParam('exception'))) + ); + + $resolver = $e->getApplication() + ->getServiceManager()->get('Zend\View\Resolver\TemplatePathStack'); + + if (false === $resolver->resolve($template = "error/$exceptionName")) { + // custom error template does not exist + return; + } + + $model = new \Zend\View\Model\ViewModel( + [ + 'display_exceptions' => APPLICATION_ENV == 'development', + 'exception' => $e->getParam('exception'), + ] + ); + $model->setTemplate($template); + $e->setResult($model); + }; + + $this->events->attach('dispatch.error', $callback); + } +} diff --git a/themes/finc/templates/error/RuntimeException.phtml b/themes/finc/templates/error/RuntimeException.phtml new file mode 100644 index 00000000000..0e8933ecf5a --- /dev/null +++ b/themes/finc/templates/error/RuntimeException.phtml @@ -0,0 +1,56 @@ +<!-- finc: RuntimeException --> +<?/* largely copied from /bootstrap3/templates/error/index.phtml #17035 - RL */?> +<?php + // Set page title. + $this->headTitle($this->translate('An error has occurred')); + + $this->layout()->breadcrumbs = '<li class="active">Error</li>'; +?> +<div class="alert alert-danger"> + <p><?=$this->transEsc('An error has occurred')?></p> + <p> + <?php if (strpos($exception->getMessage(), "nable to connect to") !== false): ?> + <?=$this->translate('error_service_unavailable')?> + <?php else: ?> + <p><?=$this->transEsc($this->message)?></p> + <?=$this->transEsc('Please contact the Library Reference Department for assistance')?> + <?php $supportEmail = $this->escapeHtmlAttr($this->systememail()); ?> + <a href="mailto:<?=$supportEmail?>"><?=$supportEmail?></a> + <?php endif; ?> + <br/> + </p> +</div> + +<?php if ($this->showInstallLink): ?> + <h2><a href="<?=$this->url('install-home')?>"><?=$this->transEsc('auto_configure_title', [], 'Auto Configure')?></a></h2> + <?=$this->transEsc('auto_configure_description', [], 'If this is a new installation, you may be able to fix the error using VuFind\'s Auto Configure tool.')?> + <h2><a href="<?=$this->url('upgrade-home')?>"><?=$this->transEsc('Upgrade VuFind')?></a></h2> + <?=$this->transEsc('upgrade_description', [], 'If you are upgrading a previous VuFind version, you can load your old settings with this tool.')?> +<?php endif; ?> + +<?php if (isset($this->display_exceptions) && $this->display_exceptions): ?> + <h2><?=$this->transEsc('Exception')?>:</h2> + <p> + <b><?=$this->transEsc('Message')?>:</b> <?=$this->escapeHtml($this->exception->getMessage())?> + </p> + + <h2><?=$this->transEsc('Backtrace')?>:</h2> + <pre><?=$this->exception->getTraceAsString()?> + </pre> + + <?php if ($e = $this->exception->getPrevious()): ?> + <h3>Previous exceptions:</h3> + <?php while ($e): ?> + <h4><?php echo get_class($e); ?></h4> + <p><?=$e->getMessage()?></p> + <pre><?=$e->getTraceAsString()?></pre> + <?php $e = $e->getPrevious(); ?> + <?php endwhile; ?> + <?php endif; ?> + + <?php if (isset($this->request)): ?> + <h2><?=$this->transEsc('error_page_parameter_list_heading')?>:</h2> + <pre><?=$this->escapeHtml(var_export($this->request->getParams(), true))?></pre> + <?php endif; ?> +<?php endif ?> +<!-- finc: RuntimeException - END --> diff --git a/themes/finc/templates/error/TimeoutException.phtml b/themes/finc/templates/error/TimeoutException.phtml new file mode 100644 index 00000000000..825b115fa32 --- /dev/null +++ b/themes/finc/templates/error/TimeoutException.phtml @@ -0,0 +1,48 @@ +<!-- finc: TimeoutException --> +<?/* largely copied from /bootstrap3/templates/error/index.phtml #17035 - RL */?> +<?php + // Set page title. + $this->headTitle($this->translate('An error has occurred')); + + $this->layout()->breadcrumbs = '<li class="active">Error</li>'; +?> +<div class="alert alert-danger"> + <p> + <?=sprintf($this->translate('error_timeout'), $this->systememail(), $this->systememail())?> + <br/> + </p> +</div> + +<?php if ($this->showInstallLink): ?> + <h2><a href="<?=$this->url('install-home')?>"><?=$this->transEsc('auto_configure_title', [], 'Auto Configure')?></a></h2> + <?=$this->transEsc('auto_configure_description', [], 'If this is a new installation, you may be able to fix the error using VuFind\'s Auto Configure tool.')?> + <h2><a href="<?=$this->url('upgrade-home')?>"><?=$this->transEsc('Upgrade VuFind')?></a></h2> + <?=$this->transEsc('upgrade_description', [], 'If you are upgrading a previous VuFind version, you can load your old settings with this tool.')?> +<?php endif; ?> + +<?php if (isset($this->display_exceptions) && $this->display_exceptions): ?> + <h2><?=$this->transEsc('Exception')?>:</h2> + <p> + <b><?=$this->transEsc('Message')?>:</b> <?=$this->escapeHtml($this->exception->getMessage())?> + </p> + + <h2><?=$this->transEsc('Backtrace')?>:</h2> + <pre><?=$this->exception->getTraceAsString()?> + </pre> + + <?php if ($e = $this->exception->getPrevious()): ?> + <h3>Previous exceptions:</h3> + <?php while ($e): ?> + <h4><?php echo get_class($e); ?></h4> + <p><?=$e->getMessage()?></p> + <pre><?= $e->getTraceAsString()?></pre> + <?php $e = $e->getPrevious(); ?> + <?php endwhile; ?> + <?php endif; ?> + + <?php if (isset($this->request)): ?> + <h2><?=$this->transEsc('error_page_parameter_list_heading')?>:</h2> + <pre><?=$this->escapeHtml(var_export($this->request->getParams(), true))?></pre> + <?php endif; ?> +<?php endif ?> +<!-- finc: TimeoutException - END --> \ No newline at end of file -- GitLab