Bugzilla – Attachment 110912 Details for
Bug 26555
Add a way for Koha::Object(s) to carry execution information
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 26555: Add ->messages and ->add_message to Koha::Object
Bug-26555-Add--messages-and--addmessage-to-KohaObj.patch (text/plain), 3.37 KB, created by
Tomás Cohen Arazi (tcohen)
on 2020-09-29 13:19:20 UTC
(
hide
)
Description:
Bug 26555: Add ->messages and ->add_message to Koha::Object
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2020-09-29 13:19:20 UTC
Size:
3.37 KB
patch
obsolete
>From fe2157fec623dce62fc2dfcc7f8a94fd8a6cfb44 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Mon, 28 Sep 2020 10:49:24 -0300 >Subject: [PATCH] Bug 26555: Add ->messages and ->add_message to Koha::Object > >This patch adds a way to make Koha::Object-derived classes to carry >messages around. This targets non-fatal errors, or around action flags >for 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 | 23 ++++++++++++++++++- > 2 files changed, 65 insertions(+), 1 deletion(-) > >diff --git a/Koha/Object.pm b/Koha/Object.pm >index bb2a5d8ee8..da9315dcc8 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::Message; > > =head1 NAME > >@@ -77,6 +78,8 @@ sub new { > $schema->resultset( $class->_type() )->new($attributes); > } > >+ $self->{_messages} = []; >+ > 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->messages >+ >+ my @messages = @{ $object->messages }; >+ >+Returns the (probably non-fatal) messages that were recorded on the object. >+ >+=cut >+ >+sub messages { >+ my ( $self ) = @_; >+ return $self->{_messages}; >+} >+ >+=head3 $object->add_message >+ >+ try { >+ <some action that might fail> >+ } >+ catch { >+ if ( <fatal condition> ) { >+ Koha::Exception->throw... >+ } >+ >+ # This is a non fatal error, notify the caller >+ $self->add_message({ message => $error, type => 'error' }); >+ } >+ return $self; >+ >+Adds a message. >+ >+=cut >+ >+sub add_message { >+ my ( $self, $params ) = @_; >+ >+ push @{ $self->{_messages} }, Koha::Object::Message->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..9031533113 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,24 @@ subtest 'set_or_blank' => sub { > > $schema->storage->txn_rollback; > }; >+ >+subtest 'messages() and add_message() tests' => sub { >+ >+ plan tests => 6; >+ >+ my $patron = Koha::Patron->new; >+ >+ my @messages = @{ $patron->messages }; >+ is( scalar @messages, 0, 'No messages' ); >+ >+ $patron->add_message({ message => "message_1" }); >+ $patron->add_message({ message => "message_2" }); >+ >+ @messages = @{ $patron->messages }; >+ >+ is( scalar @messages, 2, 'Messages are returned' ); >+ is( ref($messages[0]), 'Koha::Object::Message', 'Right type returned' ); >+ is( ref($messages[1]), 'Koha::Object::Message', 'Right type returned' ); >+ is( $messages[0]->message, 'message_1', 'Right message recorded' ); >+ is( $messages[1]->message, 'message_2', 'Right message recorded' ); >+}; >-- >2.28.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 26555
:
110854
|
110855
|
110911
|
110912
|
110913
|
110957
|
110958
|
110959
|
110967
|
110968
|
110969
|
110983
|
111426
|
111427
|
111428
|
111723