From 674fd9970f2ac87b31ff2b9b8f6dc66ae607a344 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 4 Feb 2022 10:28:49 +0000 Subject: [PATCH] Bug 29857: (CONCEPT) Switch to singular, add a load method Content-Type: text/plain; charset=utf-8 The load method calls the Class::Exception import. Allowing you to skip a use Class::Exception everywhere, and define 'nicer' exception lists? --- Koha/Exception.pm | 14 ++++--- Koha/Exception/Generic.pm | 82 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 Koha/Exception/Generic.pm diff --git a/Koha/Exception.pm b/Koha/Exception.pm index c420777cd2..826f55cacc 100644 --- a/Koha/Exception.pm +++ b/Koha/Exception.pm @@ -18,12 +18,16 @@ package Koha::Exception; # along with Koha; if not, see . use Modern::Perl; +use Exception::Class qw(); -use Exception::Class ( - 'Koha::Exception' => { - description => "Something went wrong!" - }, -); +INIT { __PACKAGE__->load; } + +sub load { + my ( $class, @exceptions ) = @_; + push @exceptions, 'Koha::Exception' => { description => "Something went wrong!" } + unless @exceptions; + Exception::Class->import(@exceptions); +} sub full_message { my $self = shift; diff --git a/Koha/Exception/Generic.pm b/Koha/Exception/Generic.pm new file mode 100644 index 0000000000..d106027ea9 --- /dev/null +++ b/Koha/Exception/Generic.pm @@ -0,0 +1,82 @@ +package Koha::Exception::Generic; + +use Modern::Perl; +use parent qw(Koha::Exception); + +my @exceptions; +INIT { __PACKAGE__->load( @exceptions ); } + +@exceptions = ( + 'Koha::Exception::BadParameter' => { + isa => 'Koha::Exception', + description => 'A bad parameter was given', + fields => ['parameter'], + }, + 'Koha::Exception::DuplicateObject' => { + isa => 'Koha::Exception', + description => 'Same object already exists', + }, + 'Koha::Exception::ObjectNotFound' => { + isa => 'Koha::Exception', + description => 'The required object doesn\'t exist', + }, + 'Koha::Exception::ObjectNotCreated' => { + isa => 'Koha::Exception', + description => 'The object have not been created', + }, + 'Koha::Exception::CannotDeleteDefault' => { + isa => 'Koha::Exception', + description => 'The default value cannot be deleted' + }, + 'Koha::Exception::MissingParameter' => { + isa => 'Koha::Exception', + description => 'A required parameter is missing' + }, + 'Koha::Exception::ParameterTooHigh' => { + isa => 'Koha::Exception', + description => 'A passed parameter value is too high' + }, + 'Koha::Exception::NoChanges' => { + isa => 'Koha::Exception', + description => 'No changes were made', + }, + 'Koha::Exception::WrongParameter' => { + isa => 'Koha::Exception', + description => 'One or more parameters are wrong', + }, + 'Koha::Exception::NoPermission' => { + isa => 'Koha::Exception', + description => 'You do not have permission for this action', + }, + 'Koha::Exception::CannotAddLibraryLimit' => { + isa => 'Koha::Exception', + description => 'General problem adding a library limit' + }, + 'Koha::Exception::UnderMaintenance' => { + isa => 'Koha::Exception', + description => 'Koha is under maintenance.' + }, + # Virtualshelves exceptions + 'Koha::Exception::Virtualshelves::DuplicateObject' => { + isa => 'Koha::Exception::DuplicateObject', + description => "Duplicate shelf object", + }, + 'Koha::Exception::Virtualshelves::InvalidInviteKey' => { + isa => 'Koha::Exception', + description => 'Invalid key on accepting the share', + }, + 'Koha::Exception::Virtualshelves::InvalidKeyOnSharing' => { + isa => 'Koha::Exception', + description=> 'Invalid key on sharing a shelf', + }, + 'Koha::Exception::Virtualshelves::ShareHasExpired' => { + isa => 'Koha::Exception', + description=> 'Cannot share this shelf, the share has expired', + }, + 'Koha::Exception::Virtualshelves::UseDbAdminAccount' => { + isa => 'Koha::Exception', + description => "Invalid use of database administrator account", + } +); + +1; -- 2.20.1