From bd2b530aeed74d0fec1fb4490b82d2046699f470 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Sun, 21 Sep 2025 12:42:50 +0100 Subject: [PATCH] Bug 40445: Use centralized exception handling in Cash Register methods Update start_cashup and add_cashup methods to use the new centralized exception translation system instead of letting raw DBIx::Class exceptions bubble up. This provides consistent Koha-specific exceptions across the API. Also update the corresponding unit tests to expect proper Koha::Exceptions::Object::FKConstraint exceptions instead of generic errors when invalid manager_id values are provided. --- Koha/Cash/Register.pm | 37 ++++++++++++++++++----------- t/db_dependent/Koha/Cash/Register.t | 13 ++++++---- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Koha/Cash/Register.pm b/Koha/Cash/Register.pm index 293021b0ef9..6651bedaf39 100644 --- a/Koha/Cash/Register.pm +++ b/Koha/Cash/Register.pm @@ -291,14 +291,19 @@ sub start_cashup { my $expected_amount = abs( $self->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ) ); - # Create the CASHUP_START action - my $rs = $self->_result->add_to_cash_register_actions( - { - code => 'CASHUP_START', - manager_id => $manager_id, - amount => $expected_amount + # Create the CASHUP_START action using centralized exception handling + my $schema = $self->_result->result_source->schema; + my $rs = $schema->safe_do( + sub { + return $self->_result->add_to_cash_register_actions( + { + code => 'CASHUP_START', + manager_id => $manager_id, + amount => $expected_amount + } + )->discard_changes; } - )->discard_changes; + ); return Koha::Cash::Register::Cashup->_new_from_dbic($rs); } @@ -387,14 +392,18 @@ sub add_cashup { $schema->txn_do( sub { - # Create the cashup action with actual amount - my $rs = $self->_result->add_to_cash_register_actions( - { - code => 'CASHUP', - manager_id => $manager_id, - amount => $amount + # Create the cashup action with actual amount using centralized exception handling + my $rs = $schema->safe_do( + sub { + return $self->_result->add_to_cash_register_actions( + { + code => 'CASHUP', + manager_id => $manager_id, + amount => $amount + } + )->discard_changes; } - )->discard_changes; + ); $cashup = Koha::Cash::Register::Cashup->_new_from_dbic($rs); diff --git a/t/db_dependent/Koha/Cash/Register.t b/t/db_dependent/Koha/Cash/Register.t index e227fb9777c..0b9fd35717f 100755 --- a/t/db_dependent/Koha/Cash/Register.t +++ b/t/db_dependent/Koha/Cash/Register.t @@ -1071,8 +1071,10 @@ subtest 'start_cashup_parameter_validation' => sub { my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); - eval { $register3->start_cashup( { manager_id => 99999999 } ); }; - ok( $@, 'start_cashup fails with invalid manager_id' ); + throws_ok { + $register3->start_cashup( { manager_id => 99999999 } ); + } + 'Koha::Exceptions::Object::FKConstraint', 'start_cashup throws FK constraint exception with invalid manager_id'; }; # Test 4: Duplicate start_cashup prevention @@ -1377,9 +1379,10 @@ subtest 'add_cashup' => sub { my $register9 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); - eval { $register9->add_cashup( { manager_id => 99999999, amount => '10.00' } ); }; - ok( $@, 'add_cashup fails with invalid manager_id' ); - diag($@); + throws_ok { + $register9->add_cashup( { manager_id => 99999999, amount => '10.00' } ); + } + 'Koha::Exceptions::Object::FKConstraint', 'add_cashup throws FK constraint exception with invalid manager_id'; }; $schema->storage->txn_rollback; -- 2.51.0