From 27c064e671320b03e04418742e46f5380f07e88e Mon Sep 17 00:00:00 2001
From: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Date: Wed, 3 Jul 2024 11:17:27 +0000
Subject: [PATCH] Bug 36942: Use an exception for a failed KBART file parse

Test plan:
1) prove t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t
---
 Koha/ERM/EHoldings/Title.pm                         | 3 +--
 Koha/Exceptions.pm                                  | 5 +++++
 t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t | 7 +++----
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/Koha/ERM/EHoldings/Title.pm b/Koha/ERM/EHoldings/Title.pm
index 73e4d53846..60e65d71db 100644
--- a/Koha/ERM/EHoldings/Title.pm
+++ b/Koha/ERM/EHoldings/Title.pm
@@ -137,8 +137,7 @@ sub read_file {
     unless ( $csv->eof() ) {
         my ( $cde, $str, $pos ) = $csv->error_diag();
         my $error = $cde ? "$cde, $str, $pos" : "";
-        warn $error if $error;
-        return ( $column_headers, $lines, $error );
+        Koha::Exceptions::FileNotParsed->throw( filename => $file->{filename}, error => $error );
     }
 
     return ( $column_headers, $lines );
diff --git a/Koha/Exceptions.pm b/Koha/Exceptions.pm
index 0d5a1ac1ed..1415c05fc7 100644
--- a/Koha/Exceptions.pm
+++ b/Koha/Exceptions.pm
@@ -20,6 +20,11 @@ use Exception::Class (
         description => 'File not found',
         fields      => ['filename'],
     },
+    'Koha::Exceptions::FileNotParsed' => {
+        isa         => 'Koha::Exception',
+        description => 'File could not be parsed',
+        fields      => [ 'filename', 'error' ],
+    },
     'Koha::Exceptions::InvalidStatus' => {
         isa         => 'Koha::Exception',
         description => 'The current status is not valid in context',
diff --git a/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t b/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t
index c018a9414b..0b8cdf6894 100755
--- a/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t
+++ b/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t
@@ -20,6 +20,7 @@ use Modern::Perl;
 use Test::More tests => 8;
 use Test::MockModule;
 use Test::Warn;
+use Test::Exception;
 
 use Koha::Database;
 use Koha::BackgroundJobs;
@@ -135,10 +136,8 @@ Nature Plants,,2055-0278,2015-01,1,1,,,,https://www.nature.com/nplants,,4aaa7,,f
 Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bbb0,,fulltext,Hybrid (Open Choice),Nature Publishing Group UK,serial,,,,,,,,P"
         )
     };
-    warning_is {
-        Koha::ERM::EHoldings::Title::read_file($file2);
-    }
-    '2023, EIQ - QUO character not allowed, 157', 'Error message correctly reported';
+
+    throws_ok { Koha::ERM::EHoldings::Title::read_file($file2); } 'Koha::Exceptions::FileNotParsed';
 };
 
 subtest '_create_title_hash_from_line_data' => sub {
-- 
2.39.3 (Apple Git-146)