Bug 42156: (QA follow-up) Guard against missing or unparseable authority records

The previous defensive block still dereferenced $target_auth unconditionally
when attempting to recover via record_strip_nonxml. If Koha::Authorities->find
returned undef (stale search index pointing at a deleted authid) the eval
around ->record would catch the first failure, but the recovery call on the
next line would die uncaught.

This patch:
 - Skips silently when the authid returned by the search engine no longer
   exists in the database, so get_matches cannot die on a stale index.
 - Wraps record_strip_nonxml in eval so an exception there is also handled
   and the record is simply skipped - matching the stated intent of the
   "skip when not possible" path.

Test plan:
 1 - Run t/db_dependent/Matcher.t - still passes.
 2 - Simulate a stale index: mock search_auth_compat to return an authid
     that is not present in auth_header. Confirm get_matches no longer dies
     and returns without error.

Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 9d2b2c2493)
Signed-off-by: Jacob O'Mara <Jacob.omara@openfifth.co.uk>
This commit is contained in:
Martin Renvoize 2026-04-21 13:46:41 +01:00 committed by Jacob O'Mara
parent 75884ec99a
commit 15878ddc15
No known key found for this signature in database
1 changed files with 4 additions and 3 deletions

View File

@ -789,14 +789,15 @@ sub get_matches {
my ( $authresults, $total ) = $searcher->search_auth_compat( $search_query, 0, 20 );
foreach my $result (@$authresults) {
my $id = $result->{authid};
my $target_auth = Koha::Authorities->find($id);
my $id = $result->{authid};
my $target_auth = Koha::Authorities->find($id);
next unless $target_auth;
my $target_record = eval { $target_auth->record };
my $invalid_record = $@ || !$target_record;
if ($invalid_record) {
warn
"Error parsing matched authority record $id, attempting to clean record, record will be skipped if it fails";
$target_record = $target_auth->record_strip_nonxml;
$target_record = eval { $target_auth->record_strip_nonxml };
}
next unless $target_record;
$matches->{$id}->{score} += $matchpoint->{'score'};