From 091bfeefc84f578c36bbfbe892169b28120079e9 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 31 Jan 2022 12:07:02 -0300 Subject: [PATCH] Bug 29857: Fix behavior This patch makes: - passed parameters be rendered alphabetically to allow testing. - only the defined parameters be rendered in the string - the method honor the fact that $self->message should be returned if passed to the constructor (i.e. if the developer called Koha::Exceptions::MyException->throw( $string ) they will expect $string to be rendered. To test: 1. Apply the unit tests patch 2. Run: $ kshell k$ prove t/Koha/Exceptions.t => FAIL: Tests fail! 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! 5. Sign off :-D Signed-off-by: Tomas Cohen Arazi --- Koha/Exception.pm | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/Koha/Exception.pm b/Koha/Exception.pm index 69702d2936..7e5b195867 100644 --- a/Koha/Exception.pm +++ b/Koha/Exception.pm @@ -10,15 +10,42 @@ use Exception::Class ( sub full_message { my $self = shift; - my $msg = $self->description; - my @fields; + + # If a message was passed manually, return it + return $self->message + if $self->message; + my $field_hash = $self->field_hash; - while ( my ( $field, $value ) = each %$field_hash ) { - push @fields, $field . " => " . $value; + + my $description = $self->description; + my @fields; + + foreach my $key ( sort keys %$field_hash ) { + push @fields, $key . " => " . $field_hash->{$key} + if defined $field_hash->{$key}; } + return sprintf "Exception '%s' thrown '%s'" . ( @fields ? " with %s" : "" ) . "\n", - ref($self), $msg, ( @fields ? join ', ', @fields : () ); + ref($self), $description, ( @fields ? join ', ', @fields : () ); } +=head1 NAME + +Koha::Exception - Base class for exceptions + +=head1 Exceptions + +=head2 Koha::Exception + +Generic exception. + +=head1 Class methods + +=head2 full_message + +Generic method for exception stringifying. + +=cut + 1; -- 2.32.0