From 33c8d7579663ff7822417ff76383dd7318dc0cb1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 19 Oct 2023 09:28:23 +0100 Subject: [PATCH] Bug 35099: Defensive coding for bad MARC21 records This patch modernises the get_marc_volumes method to match the form of get_marc_components. The original code was submitted around a similar time but didn't get the modernisations introduced since. We remove the original localised caching as well as putting an eval around the search_simple_compat call to prevent crashes on bad marc records found in the database. Signed-off-by: Martin Renvoize --- Koha/Biblio.pm | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 9e2386ae986..fc38955718c 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -728,20 +728,31 @@ this object (MARC21 773$w or 8xx$w point to this) sub get_marc_volumes { my ( $self, $max_results ) = @_; - return $self->{_volumes} if defined( $self->{_volumes} ); + return [] if ( C4::Context->preference('marcflavour') ne 'MARC21' ); my $searchstr = $self->get_volumes_query; + my $volumes; if ( defined($searchstr) ) { my $searcher = Koha::SearchEngine::Search->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); - my ( $errors, $results, $total_hits ) = $searcher->simple_search_compat( $searchstr, 0, $max_results ); - $self->{_volumes} = - ( defined($results) && scalar(@$results) ) ? $results : []; - } else { - $self->{_volumes} = []; + my ( $error, $results, $total_hits ); + eval { ( $error, $results, $total_hits ) = $searcher->simple_search_compat( $searchstr, 0, $max_results ); }; + if ( $error || $@ ) { + $error //= q{}; + $error .= $@ if $@; + warn "Warning from search_compat: '$error'"; + $self->add_message( + { + type => 'error', + message => 'volume_search', + payload => $error, + } + ); + } + $volumes = ( defined($results) && scalar(@$results) ) ? $results : []; } - return $self->{_volumes}; + return $volumes // []; } =head2 get_volumes_query -- 2.41.0