From ff3bfb53143439463c061edabb829aef2f6bc6e1 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 28 Sep 2020 10:23:50 -0300 Subject: [PATCH] Bug 26555: Add Koha::Object::Error This patch introduces a tiny class for encapsulating errors in Koha::Object derived classes. To test: 1. Apply this patch 2. Run; $ kshell k$ prove t/Koha/Object/Error.t => SUCCESS: Tests pass! 3. Sign off :-D --- Koha/Object/Error.pm | 74 +++++++++++++++++++++++++++++++++++++++++++ t/Koha/Object/Error.t | 45 ++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 Koha/Object/Error.pm create mode 100755 t/Koha/Object/Error.t diff --git a/Koha/Object/Error.pm b/Koha/Object/Error.pm new file mode 100644 index 0000000000..d8a9912437 --- /dev/null +++ b/Koha/Object/Error.pm @@ -0,0 +1,74 @@ +package Koha::Object::Error; + +# 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, see . + +use Modern::Perl; + +use base qw(Class::Accessor); + +use Koha::Exceptions; + +__PACKAGE__->mk_ro_accessors(qw( error )); + +=head1 NAME + +Koha::Object::Error - Class encapsulating errors in Koha::Object-derived classes + +=head1 SYNOPSIS + + my ($self, $params) = @_; + push @{$self->{_errors}} = Koha::Object::Error->new($params); + +=head1 API + +=head2 Class methods + +=head3 new + + my $error = Koha::Object::Error->new( + { + error => $some_error + } + ); + +Create a new Koha::Object::Error object. + +=cut + +sub new { + my ( $class, $params ) = @_; + + my $error = $params->{error}; + + Koha::Exceptions::MissingParameter->throw( "Mandatory parameter missing: 'error'" ) + unless $error; + + my $self = $class->SUPER::new( + { + error => $error + } + ); + + return $self; +} + +=head1 AUTHOR + +Tomas Cohen Arazi, Etomascohen@theke.ioE + +=cut + +1; diff --git a/t/Koha/Object/Error.t b/t/Koha/Object/Error.t new file mode 100755 index 0000000000..176670e954 --- /dev/null +++ b/t/Koha/Object/Error.t @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +# Copyright 2019 Koha Development team +# +# 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, see . + +use Modern::Perl; + +use Test::More tests => 2; +use Test::Exception; + +BEGIN { + use_ok('Koha::Object::Error'); +} + +subtest 'new() tests' => sub { + + plan tests => 4; + + my $some_error = 'Some error'; + + my $error = Koha::Object::Error->new({ error => $some_error }); + is( ref($error), 'Koha::Object::Error', 'Type is correct' ); + is( $error->error, $some_error, 'The error attribute has the right value' ); + + throws_ok + { Koha::Object::Error->new({ blah => 'ohh' }); } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if required parameter missing'; + + is( "$@", "Mandatory parameter missing: 'error'", 'Expected exception message' ); +}; -- 2.28.0