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.

Sponsored-by: OpenFifth <https://openfifth.co.uk>
Signed-off-by: Jackie Usher <jackie.usher@westsussex.gov.uk>
Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Martin Renvoize 2025-09-21 12:34:09 +01:00 committed by Lucas Gass
parent eb4a781da3
commit 17b117ca36
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
1 changed files with 2 additions and 37 deletions

View File

@ -2444,43 +2444,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 \(`(?<column>.*?)`\)/ ) {
Koha::Exceptions::Object::FKConstraint->throw(
error => 'Broken FK constraint',
broken_fk => $+{column}
);
}
} elsif ( $_->{msg} =~ /Duplicate entry '(.*?)' for key '(?<key>.*?)'/ ) {
Koha::Exceptions::Object::DuplicateID->throw(
error => 'Duplicate ID',
duplicate_id => $+{key}
);
} elsif ( $_->{msg} =~ /Incorrect (?<type>\w+) value: '(?<value>.*)' for column \W?(?<property>\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($_);
};
}