From 0017bc67907bdbd48ee3456efe377e9fec9d294d 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 --- 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 a01dd39a87..a508498182 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.24.1 (Apple Git-126)