From a5aa53fdee7951224c13fa37f7bf91c0689799a6 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 28 Sep 2020 10:49:24 -0300 Subject: [PATCH] Bug 26555: Add ->errors and ->add_error to Koha::Object This patch adds a way to make Koha::Object-derived classes to carry errors around. This targets non-fatal errors that need to be notified to the caller, to avoid complex exception handling on the controllers when it is not expected to be fatal. To test: 1. Apply this patchset 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => SUCCESS: Tests pass! 3. Sign off :-D --- Koha/Object.pm | 43 ++++++++++++++++++++++++++++++++++++ t/db_dependent/Koha/Object.t | 24 +++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index bb2a5d8ee8..3126585fde 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -28,6 +28,7 @@ use Try::Tiny; use Koha::Database; use Koha::Exceptions::Object; use Koha::DateUtils; +use Koha::Object::Error; =head1 NAME @@ -77,6 +78,8 @@ sub new { $schema->resultset( $class->_type() )->new($attributes); } + $self->{_errors} = []; + croak("No _type found! Koha::Object must be subclassed!") unless $class->_type(); @@ -332,6 +335,46 @@ sub get_from_storage { return $object_class->_new_from_dbic($stored_object); } +=head3 $object->errors + + my @errors = @{ $object->errors }; + +Returns the (probably non-fatal) errors that were recorded on the object. + +=cut + +sub errors { + my ( $self ) = @_; + return $self->{_errors}; +} + +=head3 $object->add_error + + try { + + } + catch { + if ( ) { + Koha::Exception->throw... + } + + # This is a non fatal error, notify the caller + $self->add_error({ error => $error }); + } + return $self; + +Adds an error. + +=cut + +sub add_error { + my ( $self, $params ) = @_; + + push @{ $self->{_errors} }, Koha::Object::Error->new($params); + + return $self; +} + =head3 $object->TO_JSON Returns an unblessed representation of the object, suitable for JSON output. diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index cae049cbd2..814d966efc 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 21; use Test::Exception; use Test::Warn; use DateTime; @@ -867,3 +867,25 @@ subtest 'set_or_blank' => sub { $schema->storage->txn_rollback; }; + +subtest 'errors() and add_error() tests' => sub { + + plan tests => 6; + + my $patron = Koha::Patron->new; + + my @errors = @{ $patron->errors }; + is( scalar @errors, 0, 'No errors' ); + + $patron->add_error({ error => "error_1" }); + $patron->add_error({ error => "error_2" }); + + + @errors = @{ $patron->errors }; + + is( scalar @errors, 2, 'Errors are returned' ); + is( ref($errors[0]), 'Koha::Object::Error', 'Right type returned' ); + is( ref($errors[1]), 'Koha::Object::Error', 'Right type returned' ); + is( $errors[0]->error, 'error_1', 'Right error recorded' ); + is( $errors[1]->error, 'error_2', 'Right error recorded' ); +}; -- 2.28.0