@@ -, +, @@ $ kshell k$ prove t/Koha/Exceptions.t --- Koha/Exceptions/Object.pm | 8 ++++++++ t/Koha/Exceptions.t | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) --- a/Koha/Exceptions/Object.pm +++ a/Koha/Exceptions/Object.pm @@ -23,6 +23,11 @@ use Exception::Class ( 'Koha::Exceptions::Object' => { isa => 'Koha::Exceptions::Exception', }, + 'Koha::Exceptions::Object::CannotBeDeleted' => { + isa => 'Koha::Exceptions::Object', + description => 'The object cannot be deleted', + fields => ['object', 'reason'], + }, 'Koha::Exceptions::Object::DuplicateID' => { isa => 'Koha::Exceptions::Object', description => "Duplicate ID passed", @@ -84,6 +89,9 @@ sub full_message { elsif ( $self->isa('Koha::Exceptions::Object::NotInstantiated') ) { $msg = sprintf("Tried to access the '%s' method, but %s is not instantiated", $self->method, $self->class ); } + elsif ( $self->isa('Koha::Exceptions::Object::CannotBeDeleted') ) { + $msg = sprintf("Cannot delete %s object (id=%s). Reason: %s", $self->object->_get_object_class, $self->object->id, $self->reason ); + } } return $msg; --- a/t/Koha/Exceptions.t +++ a/t/Koha/Exceptions.t @@ -17,10 +17,12 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::MockObject; use Test::Exception; +use Koha::Items; + subtest 'Koha::Exceptions::Hold tests' => sub { plan tests => 5; @@ -196,3 +198,23 @@ subtest 'Koha::Exceptions::Object::NotInstantiated tests' => sub { 'Exception is thrown :-D'; is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); }; + +subtest 'Koha::Exceptions::Object::CannotBeDeleted tests' => sub { + + plan tests => 4; + + throws_ok + { Koha::Exceptions::Object::CannotBeDeleted->throw( + object => Koha::Item->new({ itemnumber => 1234 }), reason => 'Some reason' ); } + 'Koha::Exceptions::Object::CannotBeDeleted', + 'Exception is thrown :-D'; + + # stringify the exception + like( "$@", qr/Cannot delete Koha::Item=HASH\(.*\) object \(id=1234\). Reason: Some reason/ ); + + throws_ok + { Koha::Exceptions::Object::CannotBeDeleted->throw( "Manual message exception" ) } + 'Koha::Exceptions::Object::CannotBeDeleted', + 'Exception is thrown :-D'; + is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); +}; --