From 27793b89797fa3bdce7c39d70c020fbabb44fb1b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 2 Nov 2021 10:43:57 +0100 Subject: [PATCH] Bug 29387: Stringify exception when logging error during a batch mod biblio MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit If an exception is raised by one the methods/subroutines called when batch biblio update, the job will fail in the middle and some records won't be processed. Test plan: 0. Don't apply the patch In KTD you can run the a batch mod biblio on the 100 first records and the worker will fail with """ countered object 'C4::Biblio::_koha_modify_biblioitem_nonmarc(): DBI Exception: DBD::mysql::st execute failed: Data too long for column 'lccn' at row 1 at /kohadevbox/koha/C4/Biblio.pm line 423 ', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing) at /kohadevbox/koha/Koha/BackgroundJob/BatchUpdateBiblio.pm line 120. """ The UI is not saying anything about this problem. Generate 100 biblionumbers: perl -e 'for (1..100){print "$_\n"}' 1. Apply the patch and confirm than now you see the detail of the batch update and that biblionumber 72 is marked as not been modified with the correct error (raw DBI error) JK: add executable bit to BatchUpdateBiblio.t Signed-off-by: Joonas Kylmälä Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- Koha/BackgroundJob/BatchUpdateBiblio.pm | 2 +- .../Koha/BackgroundJobs/BatchUpdateBiblio.t | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index 8ab1000837..386512bdbe 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -100,7 +100,7 @@ sub process { type => 'error', code => 'biblio_not_modified', biblionumber => $biblionumber, - error => ($@ ? $@ : $error), + error => ($@ ? "$@" : $error), }; } else { push @messages, { diff --git a/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t b/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t new file mode 100755 index 0000000000..75ebf2bc2f --- /dev/null +++ b/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t @@ -0,0 +1,72 @@ +#!/usr/bin/perl + +# Copyright 2021 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::MockModule; +use JSON qw( encode_json decode_json ); + +use Koha::Database; +use Koha::BackgroundJobs; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; + +my $builder = t::lib::TestBuilder->new; + +subtest "Exceptions must be stringified" => sub { + plan tests => 1; + + $schema->storage->txn_begin; + + my $C4_biblio_module = Test::MockModule->new('C4::Biblio'); + $C4_biblio_module->mock( 'ModBiblio', + sub { Koha::Exceptions::Exception->throw("It didn't work"); } ); + + my $biblio = $builder->build_sample_biblio; + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $job = Koha::BackgroundJob::BatchUpdateBiblio->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'batch_biblio_record_modification', + data => encode_json { + record_ids => [ $biblio->biblionumber ], + } + } + )->store; + $job = Koha::BackgroundJobs->find( $job->id ); + $job->process( + { job_id => $job->id, record_ids => [ $biblio->biblionumber ] } ); + + my $data = decode_json $job->get_from_storage->data; + is_deeply( + $data->{messages}->[0], + { + biblionumber => $biblio->biblionumber, + code => 'biblio_not_modified', + error => "It didn't work", + type => "error" + } + ); + + $schema->storage->txn_rollback; +}; -- 2.39.5