From e2da96dabba83bd9386ba427d8bbbaa486f02dc2 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Sun, 21 Sep 2025 15:35:34 +0100 Subject: [PATCH] Bug 19871: Update Koha::Patron::Modification to use centralized exception handling Replace manual DBIx::Class::Exception handling in the approve() method with the centralized exception translation system while preserving domain-specific exception behavior. Changes: - Use $schema->safe_do() for database operations with automatic exception translation - Wrap safe_do in try/catch to convert any exception to domain-specific Patron::Modification exception - Simplify exception handling logic while maintaining the same API contract - Remove manual DBIx::Class::Exception type checking This maintains backward compatibility by always throwing Koha::Exceptions::Patron::Modification exceptions as before, but leverages the centralized system for consistent database exception handling. --- Koha/Patron/Modification.pm | 67 +++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/Koha/Patron/Modification.pm b/Koha/Patron/Modification.pm index 5c36a71cbca..0939eb0f815 100644 --- a/Koha/Patron/Modification.pm +++ b/Koha/Patron/Modification.pm @@ -111,43 +111,46 @@ sub approve { }; } - $self->_result->result_source->schema->txn_do( + my $schema = $self->_result->result_source->schema; + $schema->txn_do( sub { try { - $patron->store(); - - # Deal with attributes - my @codes = uniq( map { $_->{code} } @{$extended_attributes} ); - foreach my $code (@codes) { - map { $_->delete } Koha::Patron::Attributes->search( - { - borrowernumber => $patron->borrowernumber, - code => $code + $schema->safe_do( + sub { + $patron->store(); + + # Deal with attributes + my @codes = uniq( map { $_->{code} } @{$extended_attributes} ); + foreach my $code (@codes) { + map { $_->delete } Koha::Patron::Attributes->search( + { + borrowernumber => $patron->borrowernumber, + code => $code + } + )->as_list; } - )->as_list; - } - foreach my $attr ( @{$extended_attributes} ) { - $attr->{attribute} = exists $attr->{attribute} ? $attr->{attribute} : $attr->{value}; - Koha::Patron::Attribute->new( - { - borrowernumber => $patron->borrowernumber, - code => $attr->{code}, - attribute => $attr->{attribute}, + foreach my $attr ( @{$extended_attributes} ) { + $attr->{attribute} = exists $attr->{attribute} ? $attr->{attribute} : $attr->{value}; + Koha::Patron::Attribute->new( + { + borrowernumber => $patron->borrowernumber, + code => $attr->{code}, + attribute => $attr->{attribute}, + } + )->store + if $attr->{attribute} # there's a value + or ( + defined $attr->{attribute} # there's a value that is 0, and not + && $attr->{attribute} ne "" # the empty string which means delete + && $attr->{attribute} == 0 + ); } - )->store - if $attr->{attribute} # there's a value - or ( - defined $attr->{attribute} # there's a value that is 0, and not - && $attr->{attribute} ne "" # the empty string which means delete - && $attr->{attribute} == 0 - ); - } + } + ); } catch { - if ( $_->isa('DBIx::Class::Exception') ) { - Koha::Exceptions::Patron::Modification->throw( $_->{msg} ); - } else { - Koha::Exceptions::Patron::Modification->throw($_); - } + + # Convert any exception to domain-specific exception + Koha::Exceptions::Patron::Modification->throw($_); }; } ); -- 2.51.0