From 23e3db75f37be681eb74427a5de195e6da1684fe Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Sun, 21 Sep 2025 12:34:09 +0100 Subject: [PATCH] Bug 19871: Use centralized exception translation in Koha::Item::add_to_bundle Replace duplicated DBIx::Class exception handling code with the new centralized exception translation system introduced in the previous commit. This change: - Eliminates ~40 lines of duplicated exception handling code - Uses the new Koha::Schema::translate_exception() method - Maintains identical exception behavior and error messages - Addresses the FIXME comment requesting this refactoring The add_to_bundle method now uses consistent exception translation with the rest of the codebase, reducing maintenance burden and ensuring consistent error handling patterns. --- Koha/Item.pm | 39 ++------------------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index c675d0638e4..1e615b18f6a 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -2326,43 +2326,8 @@ sub add_to_bundle { ); } catch { - # FIXME: See if we can move the below copy/paste from Koha::Object::store into it's own class and catch at a lower level in the Schema instantiation, take inspiration from DBIx::Error - if ( ref($_) eq 'DBIx::Class::Exception' ) { - if ( $_->{msg} =~ /Cannot add or update a child row: a foreign key constraint fails/ ) { - - # FK constraints - # FIXME: MySQL error, if we support more DB engines we should implement this for each - if ( $_->{msg} =~ /FOREIGN KEY \(`(?.*?)`\)/ ) { - Koha::Exceptions::Object::FKConstraint->throw( - error => 'Broken FK constraint', - broken_fk => $+{column} - ); - } - } elsif ( $_->{msg} =~ /Duplicate entry '(.*?)' for key '(?.*?)'/ ) { - Koha::Exceptions::Object::DuplicateID->throw( - error => 'Duplicate ID', - duplicate_id => $+{key} - ); - } elsif ( $_->{msg} =~ /Incorrect (?\w+) value: '(?.*)' for column \W?(?\S+)/ ) - { # The optional \W in the regex might be a quote or backtick - my $type = $+{type}; - my $value = $+{value}; - my $property = $+{property}; - $property =~ s/['`]//g; - Koha::Exceptions::Object::BadValue->throw( - type => $type, - value => $value, - property => $property =~ /(\w+\.\w+)$/ - ? $1 - : $property, # results in table.column without quotes or backtics - ); - } - - # Catch-all for foreign key breakages. It will help find other use cases - $_->rethrow(); - } else { - $_->rethrow(); - } + # Use centralized exception translation instead of duplicated code + $schema->translate_exception($_); }; } -- 2.51.0