Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.21% covered (warning)
86.21%
25 / 29
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ErrorListener
86.21% covered (warning)
86.21%
25 / 29
33.33% covered (danger)
33.33%
1 / 3
15.59
0.00% covered (danger)
0.00%
0 / 1
 onSearchError
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 analyzeJsonErrorResponse
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 getResponseBodyMediaType
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
5.26
1<?php
2
3/**
4 * SOLR 4.x error listener.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2013.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Search
25 * @author   David Maus <maus@hab.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Search\Solr\V4;
31
32use Laminas\EventManager\EventInterface;
33use Laminas\Http\Response;
34use VuFind\Search\Solr\AbstractErrorListener;
35use VuFindSearch\Backend\Exception\HttpErrorException;
36
37/**
38 * SOLR 3.x error listener.
39 *
40 * @category VuFind
41 * @package  Search
42 * @author   David Maus <maus@hab.de>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 */
46class ErrorListener extends AbstractErrorListener
47{
48    /**
49     * Normalized media types.
50     *
51     * @var string
52     */
53    public const TYPE_OTHER = 'other';
54    public const TYPE_JSON  = 'json';
55    public const TYPE_XML   = 'xml';
56
57    /**
58     * VuFindSearch.error
59     *
60     * @param EventInterface $event Event
61     *
62     * @return EventInterface
63     */
64    public function onSearchError(EventInterface $event)
65    {
66        $command = $event->getParam('command');
67        if ($this->listenForBackend($command->getTargetIdentifier())) {
68            $error = $event->getParam('error');
69            if ($error instanceof HttpErrorException) {
70                $response = $error->getResponse();
71
72                $body = $response->getBody();
73                $type = $this->getResponseBodyMediaType($response);
74
75                if ($type === self::TYPE_JSON) {
76                    $body = json_decode($body);
77                    if (json_last_error() === \JSON_ERROR_NONE) {
78                        $tags = $this->analyzeJsonErrorResponse($body);
79                        foreach ($tags as $tag) {
80                            $error->addTag($tag);
81                        }
82                    }
83                }
84            }
85        }
86        return $event;
87    }
88
89    /// Internal API
90
91    /**
92     * Analyze JSON-encoded error response and return appropriate tags.
93     *
94     * @param StdLib $body Deserialize JSON body
95     *
96     * @return array Tags
97     */
98    protected function analyzeJsonErrorResponse($body)
99    {
100        $tags = [];
101        if (isset($body->error->msg)) {
102            $reason = $body->error->msg;
103            if (
104                stristr($reason, 'org.apache.solr.search.SyntaxError')
105                || stristr($reason, 'undefined field')
106                || stristr($reason, 'invalid date')
107            ) {
108                $tags[] = self::TAG_PARSER_ERROR;
109            }
110        }
111        return $tags;
112    }
113
114    /**
115     * Return normalized media type identifier.
116     *
117     * @param Response $response HTTP response
118     *
119     * @return string One of `json', `xml', or `other'
120     */
121    protected function getResponseBodyMediaType(Response $response)
122    {
123        if ($response->getHeaders()->has('content-type')) {
124            $type = $response->getHeaders()->get('content-type')->getFieldValue();
125            if (str_starts_with($type, 'application/json')) {
126                return self::TYPE_JSON;
127            }
128            if (str_starts_with($type, 'application/xml')) {
129                return self::TYPE_XML;
130            }
131        }
132        return self::TYPE_OTHER;
133    }
134}