From 5ca4e7f2b2066780b05293fa4bf1ae3c3bea9be7 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 3 Nov 2020 08:55:04 -0500 Subject: [PATCH] Bug 14708: (QA follow-up) Use try/catch blocks when calling delete() on a patron Signed-off-by: Jonathan Druart (cherry picked from commit 1e37d9c5f7af0f7e0efb226c171da52f03bc80eb) Signed-off-by: Andrew Fuerste-Henry --- Koha/REST/V1/Patrons.pm | 22 +++++++++++++++++----- members/deletemem.pl | 11 +++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 04e7a31f64..c3916d6414 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -310,11 +310,23 @@ sub delete { $patron = Koha::Patrons->find( $c->validation->param('patron_id') ); # check if loans, reservations, debarrment, etc. before deletion! - $patron->delete; - return $c->render( - status => 204, - openapi => q{} - ); + try { + $patron->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } catch { + if ( $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) { + return $c->render( + status => 403, + openapi => { error => "Anonymous patron cannot be deleted" } + ); + } + else { + $c->unhandled_exception($_); + } + }; } catch { unless ($patron) { diff --git a/members/deletemem.pl b/members/deletemem.pl index e7d6d1c271..e73231ba8a 100755 --- a/members/deletemem.pl +++ b/members/deletemem.pl @@ -24,6 +24,9 @@ use Modern::Perl; use CGI qw ( -utf8 ); + +use Try::Tiny; + use C4::Context; use C4::Output; use C4::Auth; @@ -127,9 +130,13 @@ if ( $op eq 'delete_confirm' or $countissues > 0 or $debits or $is_guarantor ) { }); my $patron = Koha::Patrons->find( $member ); $patron->move_to_deleted; - $patron->delete; + try { + $patron->delete; + print $input->redirect("/cgi-bin/koha/members/members-home.pl"); + } catch { + print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON"); + } # TODO Tell the user everything went ok - print $input->redirect("/cgi-bin/koha/members/members-home.pl"); exit 0; # Exit without error } -- 2.39.5