Bug 42391: (follow-up) Rename ExceptionTranslator to ExceptionMapper
The Koha::Schema::Util:: namespace adds unnecessary nesting for a single-purpose class. This follow-up moves it to Koha::Schema::ExceptionMapper, which sits alongside Koha::Schema itself — a better fit given it's tightly coupled to the Schema's exception_action hook. Changes: - Koha::Schema::Util::ExceptionTranslator → Koha::Schema::ExceptionMapper - Update Koha::Schema to use the new module - Move test file accordingly - Remove now-empty Koha/Schema/Util/ directory Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io> Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
parent
033cfd772c
commit
62e003b88f
|
|
@ -15,7 +15,7 @@ __PACKAGE__->load_namespaces;
|
|||
# Created by DBIx::Class::Schema::Loader v0.07025 @ 2013-10-14 20:56:21
|
||||
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:oDUxXckmfk6H9YCjW8PZTw
|
||||
|
||||
use Koha::Schema::Util::ExceptionTranslator;
|
||||
use Koha::Schema::ExceptionMapper;
|
||||
|
||||
=head1 UTILITY METHODS
|
||||
|
||||
|
|
@ -43,7 +43,7 @@ sub connection {
|
|||
|
||||
# Attempt translation — throws a Koha exception on match
|
||||
# Handles both DBI errors and DBIC internal errors (e.g. "Not in database")
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
|
||||
# No match — wrap DBI errors so DBIx::Class::Exception never leaks
|
||||
if ( $msg =~ /^DBI Exception:/ ) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package Koha::Schema::Util::ExceptionTranslator;
|
||||
package Koha::Schema::ExceptionMapper;
|
||||
|
||||
# Copyright 2025 Koha Development team
|
||||
#
|
||||
|
|
@ -25,11 +25,11 @@ use Koha::Exceptions::Object;
|
|||
|
||||
=head1 NAME
|
||||
|
||||
Koha::Schema::Util::ExceptionTranslator - Centralized DBIx::Class exception translation
|
||||
Koha::Schema::ExceptionMapper - Centralized DBIx::Class exception translation
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use Koha::Schema::Util::ExceptionTranslator;
|
||||
use Koha::Schema::ExceptionMapper;
|
||||
|
||||
# Called automatically via Koha::Schema's exception_action hook.
|
||||
# No manual invocation needed in normal code.
|
||||
|
|
@ -45,7 +45,7 @@ are automatically translated before they propagate to callers.
|
|||
|
||||
=head2 translate_exception
|
||||
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
|
||||
Attempts to translate a database error message into a Koha exception and
|
||||
throw it. If the message does not match any known pattern, returns without
|
||||
18
t/db_dependent/Koha/Schema/Util/ExceptionTranslator.t → t/db_dependent/Koha/Schema/ExceptionMapper.t
Executable file → Normal file
18
t/db_dependent/Koha/Schema/Util/ExceptionTranslator.t → t/db_dependent/Koha/Schema/ExceptionMapper.t
Executable file → Normal file
|
|
@ -23,7 +23,7 @@ use Test::More tests => 11;
|
|||
use Test::Exception;
|
||||
|
||||
use Koha::Database;
|
||||
use Koha::Schema::Util::ExceptionTranslator;
|
||||
use Koha::Schema::ExceptionMapper;
|
||||
|
||||
use t::lib::TestBuilder;
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ subtest 'foreign_key_constraint_translation' => sub {
|
|||
"Cannot add or update a child row: a foreign key constraint fails (`koha`.`items`, CONSTRAINT `items_ibfk_1` FOREIGN KEY (`biblionumber`) REFERENCES `biblio` (`biblionumber`))";
|
||||
|
||||
throws_ok {
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
}
|
||||
'Koha::Exceptions::Object::FKConstraint', 'FK constraint exception is properly translated';
|
||||
|
||||
|
|
@ -56,7 +56,7 @@ subtest 'duplicate_key_translation' => sub {
|
|||
my $msg = "Duplicate entry 'test\@example.com' for key 'borrowers.email'";
|
||||
|
||||
throws_ok {
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
}
|
||||
'Koha::Exceptions::Object::DuplicateID', 'Duplicate key exception is properly translated';
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ subtest 'bad_value_translation' => sub {
|
|||
my $msg = "Incorrect datetime value: '2025-13-45' for column 'date_due' at row 1";
|
||||
|
||||
throws_ok {
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
}
|
||||
'Koha::Exceptions::Object::BadValue', 'Bad value exception is properly translated';
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ subtest 'enum_truncation_translation' => sub {
|
|||
my $msg = "Data truncated for column 'status' at row 1";
|
||||
|
||||
throws_ok {
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
}
|
||||
'Koha::Exceptions::Object::BadValue', 'Enum truncation exception is properly translated';
|
||||
|
||||
|
|
@ -105,7 +105,7 @@ subtest 'unmatched_message_returns' => sub {
|
|||
$schema->storage->txn_begin;
|
||||
|
||||
my $msg = "Some unknown database error at row 1";
|
||||
my $result = Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
my $result = Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
|
||||
is( $result, undef, 'Unmatched message returns without throwing' );
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ subtest 'fk_constraint_deletion_translation' => sub {
|
|||
"Cannot delete or update a parent row: a foreign key constraint fails (`koha`.`items`, CONSTRAINT `items_ibfk_1` FOREIGN KEY (`biblionumber`) REFERENCES `biblio` (`biblionumber`))";
|
||||
|
||||
throws_ok {
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception($msg);
|
||||
Koha::Schema::ExceptionMapper->translate_exception($msg);
|
||||
}
|
||||
'Koha::Exceptions::Object::FKConstraintDeletion', 'FK constraint deletion exception is properly translated';
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ subtest 'not_null_translation' => sub {
|
|||
$schema->storage->txn_begin;
|
||||
|
||||
throws_ok {
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception("Column 'host' cannot be null");
|
||||
Koha::Schema::ExceptionMapper->translate_exception("Column 'host' cannot be null");
|
||||
}
|
||||
'Koha::Exceptions::Object::NotNull', 'NOT NULL violation is properly translated';
|
||||
|
||||
|
|
@ -151,7 +151,7 @@ subtest 'not_in_database_translation' => sub {
|
|||
$schema->storage->txn_begin;
|
||||
|
||||
throws_ok {
|
||||
Koha::Schema::Util::ExceptionTranslator->translate_exception("Not in database");
|
||||
Koha::Schema::ExceptionMapper->translate_exception("Not in database");
|
||||
}
|
||||
'Koha::Exceptions::Object::NotInStorage', 'Not in database is properly translated';
|
||||
|
||||
Loading…
Reference in New Issue