From 9d2b2c2493e3f743f29dfcb479214539cc4874c6 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 21 Apr 2026 13:46:41 +0100 Subject: [PATCH] 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 Signed-off-by: Lucas Gass --- C4/Matcher.pm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/C4/Matcher.pm b/C4/Matcher.pm index d7d6dc091a..ae0dd6a3f5 100644 --- a/C4/Matcher.pm +++ b/C4/Matcher.pm @@ -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'};