Skip to content

Commit

Permalink
Avoid error with MLT when record does not exist (vufind-org#3963)
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala authored Sep 26, 2024
1 parent 8da3fcc commit 0ee7d2b
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0ee7d2b

Please sign in to comment.