From 0ee7d2bcac0a58206c4835f1d087bb475ac7a349 Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Thu, 26 Sep 2024 10:33:17 +0300 Subject: [PATCH] Avoid error with MLT when record does not exist (#3963) --- .../VuFindSearch/Backend/Solr/Connector.php | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php index 6a223524c0c..18dbc3b5a57 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php @@ -227,7 +227,16 @@ public function similar($id, ParamBag $params) { $handler = $this->map->getHandler(__FUNCTION__); $this->map->prepare(__FUNCTION__, $params); - return $this->query($handler, $params, true); + + try { + return $this->query($handler, $params, true); + } catch (RequestErrorException $e) { + // If Solr was unable to fetch the record, just act like we have no similar records: + if (str_contains($e->getMessage(), 'Could not fetch document with id')) { + return '{}'; + } + throw $e; + } } /** @@ -494,6 +503,19 @@ protected function send(HttpClient $client) ); if (!$response->isSuccess()) { + // Return a more detailed error message for a 400 error: + if ($response->getStatusCode() === 400) { + $json = json_decode($response->getBody(), true); + $msgParts = ['400', $response->getReasonPhrase()]; + if ($msg = $json['error']['msg'] ?? '') { + $msgParts[] = $msg; + } + throw new RequestErrorException( + implode(' ', $msgParts), + 400, + $response + ); + } throw HttpErrorException::createFromResponse($response); } return $response->getBody();