From ec6e9eb737e3652709661dd23395a5faff2bc921 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 27 Dec 2021 11:37:59 -0300 Subject: [PATCH] Bug 29772: Make DELETE /patrons/:patron_id return error codes This patch makes the controller add the error_code attribute on error conditions payload. To test: 1. Apply the regression tests patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/patrons.t => FAIL: Boo, the API doesn't include the error_code attribute on the error responses for the DELETE /patrons/:patron_id route 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! error_code is included in the response! 5. Sign off :-D Signed-off-by: David Nind --- Koha/REST/V1/Patrons.pm | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 469d9536b4..f1759b2f39 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -23,6 +23,7 @@ use Koha::Database; use Koha::Exceptions; use Koha::Patrons; +use List::MoreUtils qw(any); use Scalar::Util qw( blessed ); use Try::Tiny qw( catch try ); @@ -340,25 +341,20 @@ sub delete { Koha::Exceptions::Exception->throw('Koha::Patron->safe_to_delete returned false but carried no error message'); } - if ( $error->message eq 'has_checkouts' ) { - return $c->render( - status => 409, - openapi => { error => 'Pending checkouts prevent deletion' } - ); - } elsif ( $error->message eq 'has_debt' ) { - return $c->render( - status => 409, - openapi => { error => 'Pending debts prevent deletion' } - ); - } elsif ( $error->message eq 'has_guarantees' ) { + my $error_descriptions = { + has_checkouts => 'Pending checkouts prevent deletion', + has_debt => 'Pending debts prevent deletion', + has_guarantees => 'Patron is a guarantor and it prevents deletion', + is_anonymous_patron => 'Anonymous patron cannot be deleted', + }; + + if ( any { $error->message eq $_ } keys %{$error_descriptions} ) { return $c->render( status => 409, - openapi => { error => 'Patron is a guarantor and it prevents deletion' } - ); - } elsif ( $error->message eq 'is_anonymous_patron' ) { - return $c->render( - status => 403, - openapi => { error => 'Anonymous patron cannot be deleted' } + openapi => { + error => $error_descriptions->{ $error->message }, + error_code => $error->message, + } ); } else { Koha::Exceptions::Exception->throw( 'Koha::Patron->safe_to_delete carried an unexpected message: ' . $error->message ); @@ -377,12 +373,6 @@ sub delete { } ); } catch { - if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) { - return $c->render( - status => 403, - openapi => { error => "Anonymous patron cannot be deleted" } - ); - } $c->unhandled_exception($_); }; -- 2.30.2