From d73bf53a9a7d673f7c6a6ff116391075feff35cd Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 23 Jan 2019 13:10:14 -0300 Subject: [PATCH] Bug 22194: Add Koha::Exceptions::Metadata This trivial patch adds a basic Koha::Exceptions::Metadata exception and Koha::Exceptions::Metadata::Invalid for using when the data cannot be decoded (maybe because of incompatibility between format, schema, or just bad data). To test: - Apply this patch - Run: $ kshell k$ prove t/Koha/Exceptions.t => SUCCESS: Tests pass! - Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- Koha/Exceptions/Metadata.pm | 69 +++++++++++++++++++++++++++++++++++++ t/Koha/Exceptions.t | 29 +++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 Koha/Exceptions/Metadata.pm diff --git a/Koha/Exceptions/Metadata.pm b/Koha/Exceptions/Metadata.pm new file mode 100644 index 0000000000..d9f55efd56 --- /dev/null +++ b/Koha/Exceptions/Metadata.pm @@ -0,0 +1,69 @@ +package Koha::Exceptions::Metadata; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Exception::Class ( + + 'Koha::Exceptions::Metadata' => { + description => 'Something went wrong!', + }, + 'Koha::Exceptions::Metadata::Invalid' => { + isa => 'Koha::Exceptions::Metadata', + description => 'Invalid data', + fields => ['id','format','schema'] + } +); + +sub full_message { + my $self = shift; + + my $msg = $self->message; + + unless ($msg) { + if ( $self->isa('Koha::Exceptions::Metadata::Invalid') ) { + $msg = sprintf( "Invalid data, cannot decode object (id=%s, format=%s, schema=%s)", + $self->id, $self->format, $self->schema ); + } + } + + return $msg; +} + +=head1 NAME + +Koha::Exceptions::Metadata - Base class for metadata exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Metadata + +Generic metadata exception + +=head2 Koha::Exceptions::Metadata::Invalid + +The metadata is invalid. + +=head1 Class methods + +=head2 full_message + +Overloaded method for exception stringifying. + +=cut + +1; diff --git a/t/Koha/Exceptions.t b/t/Koha/Exceptions.t index 7ecc115cfd..82aa496cb3 100644 --- a/t/Koha/Exceptions.t +++ b/t/Koha/Exceptions.t @@ -17,7 +17,8 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; +use Test::MockObject; use Test::Exception; subtest 'Koha::Exceptions::Object::FKConstraint tests' => sub { @@ -61,3 +62,29 @@ subtest 'Koha::Exceptions::Password tests' => sub { 'Exception is thrown :-D'; is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); }; + +subtest 'Koha::Exceptions::Metadata tests' => sub { + + plan tests => 5; + + use_ok('Koha::Exceptions::Metadata'); + + my $object = Test::MockObject->new; + $object->mock( 'id', 'an_id' ); + $object->mock( 'format', 'a_format' ); + $object->mock( 'schema', 'a_schema' ); + + throws_ok + { Koha::Exceptions::Metadata::Invalid->throw( id => 'an_id', format => 'a_format', schema => 'a_schema' ); } + 'Koha::Exceptions::Metadata::Invalid', + 'Exception is thrown :-D'; + + # stringify the exception + is( "$@", 'Invalid data, cannot decode object (id=an_id, format=a_format, schema=a_schema)', 'Exception stringified correctly' ); + + throws_ok + { Koha::Exceptions::Metadata::Invalid->throw( "Manual message exception" ) } + 'Koha::Exceptions::Metadata::Invalid', + 'Exception is thrown :-D'; + is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); +}; -- 2.17.2 (Apple Git-113)