Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CatchIlsExceptionsTrait
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 onDispatch
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * VuFind Action Feature Trait - Catch ILS exceptions from actions with an OnDispatch
5 * handler
6 *
7 * PHP version 8
8 *
9 * Copyright (C) The National Library of Finland 2022.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Controller_Plugins
26 * @author   Ere Maijala <ere.maijala@helsinki.fi>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org Main Page
29 */
30
31namespace VuFind\Controller\Feature;
32
33use Laminas\Mvc\Exception\DomainException;
34use VuFind\Exception\ILS as ILSException;
35
36/**
37 * VuFind Action Feature Trait - Catch ILS exceptions from actions with an OnDispatch
38 * handler
39 *
40 * @category VuFind
41 * @package  Controller_Plugins
42 * @author   Ere Maijala <ere.maijala@helsinki.fi>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Page
45 */
46trait CatchIlsExceptionsTrait
47{
48    /**
49     * Optional custom exception response
50     *
51     * If set, this is returned on exception instead of a default ViewModel
52     *
53     * @var mixed
54     */
55    protected $ilsExceptionResponse = null;
56
57    /**
58     * Execute the request
59     *
60     * @param \Laminas\Mvc\MvcEvent $event Event
61     *
62     * @return mixed
63     * @throws DomainException
64     */
65    public function onDispatch(\Laminas\Mvc\MvcEvent $event)
66    {
67        // Catch any ILSExceptions thrown during processing and display a generic
68        // failure message to the user (instead of going to the fatal exception
69        // screen). This offers a slightly more forgiving experience when there is
70        // an unexpected ILS issue. Note that most ILS exceptions are handled at a
71        // lower level in the code (see \VuFind\ILS\Connection and the config.ini
72        // loadNoILSOnFailure setting), but there are some rare edge cases (for
73        // example, when the MultiBackend driver fails over to NoILS while used in
74        // combination with MultiILS authentication) that could lead here.
75        try {
76            return parent::onDispatch($event);
77        } catch (ILSException $exception) {
78            // Always display generic message:
79            $this->flashMessenger()->addErrorMessage('ils_connection_failed');
80            // In development mode, also show technical failure message:
81            if ('development' == APPLICATION_ENV) {
82                $this->flashMessenger()->addErrorMessage($exception->getMessage());
83            }
84            $actionResponse = $this->ilsExceptionResponse ?? $this->createViewModel();
85            $event->setResult($actionResponse);
86            return $actionResponse;
87        }
88    }
89}