From a565f74c42e7c65e37fa0ba74ce320e73ab8857c Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 24 Oct 2023 11:51:29 +0000
Subject: [PATCH] Bug 35141: Catch ES search exceptions in
 link_bibs_to_authorities, warn, and continue

This patch simplay adds an eval and a warning to link_bibs_to_authorities to catch any errors while searching and continue linking.

To test:
1 - edit /etc/kohia/sites/kohadev/koha-conf.xml and add to elasticsearch stanza
    <request_timeout>0</request_timeout>
2 - perl misc/link_bibs_to_authorities.pl
3 - It dies immediately
4 - Apply patch
5 - perl misc/link_bibs_to_authorities.pl
6 - Now it tries every record, throwing warnings along the way, but completes

Signed-off-by: David Nind <david@davidnind.com>
---
 misc/link_bibs_to_authorities.pl | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/misc/link_bibs_to_authorities.pl b/misc/link_bibs_to_authorities.pl
index f7cd876d26..08550c76c0 100755
--- a/misc/link_bibs_to_authorities.pl
+++ b/misc/link_bibs_to_authorities.pl
@@ -211,8 +211,17 @@ sub process_bib {
 
     my $frameworkcode = GetFrameworkCode($biblionumber);
 
-    my ( $headings_changed, $results ) =
-      LinkBibHeadingsToAuthorities( $linker, $record, $frameworkcode, $allowrelink, $tagtolink );
+    my ( $headings_changed, $results );
+
+    eval {
+        ( $headings_changed, $results ) =
+            LinkBibHeadingsToAuthorities( $linker, $record, $frameworkcode, $allowrelink, $tagtolink );
+    };
+    if ($@) {
+        warn "Error while searching for authorities for biblionumber $biblionumber at " . localtime(time);
+        return;
+    }
+
     foreach my $key ( keys %{ $results->{'unlinked'} } ) {
         $unlinked_headings{$key} += $results->{'unlinked'}->{$key};
     }
-- 
2.30.2