From 9b0196b7b6f9c11a575bacc96bba6a35b5bfbc5f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 19 Aug 2024 11:16:00 -0300 Subject: [PATCH] Bug 37513: Handle Koha::Exceptions::Object::FKConstraintDeletio in controller Signed-off-by: David Nind Signed-off-by: Matt Blenkinsop Signed-off-by: Katrin Fischer --- Koha/REST/V1/RecordSources.pm | 12 +++++++++++- api/v1/swagger/paths/record_sources.yaml | 7 +++++++ t/db_dependent/api/v1/record_sources.t | 10 +++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Koha/REST/V1/RecordSources.pm b/Koha/REST/V1/RecordSources.pm index 46ce01b50f..d51eaeacda 100644 --- a/Koha/REST/V1/RecordSources.pm +++ b/Koha/REST/V1/RecordSources.pm @@ -23,7 +23,8 @@ use Mojo::Base 'Mojolicious::Controller'; use Koha::RecordSources; -use Try::Tiny qw( catch try ); +use Scalar::Util qw( blessed ); +use Try::Tiny qw( catch try ); =head1 API @@ -121,6 +122,15 @@ sub delete { $source->delete; return $c->render_resource_deleted; } catch { + if ( blessed($_) && ref($_) eq 'Koha::Exceptions::Object::FKConstraintDeletion' ) { + return $c->render( + status => 409, + openapi => { + error => 'Cannot delete record source linked to existing records', + error_code => 'cannot_delete_used', + } + ); + } $c->unhandled_exception($_); }; } diff --git a/api/v1/swagger/paths/record_sources.yaml b/api/v1/swagger/paths/record_sources.yaml index 1e3da46273..7dbbf589d8 100644 --- a/api/v1/swagger/paths/record_sources.yaml +++ b/api/v1/swagger/paths/record_sources.yaml @@ -233,6 +233,13 @@ description: Not found schema: $ref: "../swagger.yaml#/definitions/error" + "409": + description: | + Conflict in deleting resource. Possible `error_code` attribute values: + + * `cannot_delete_used`: record source linked to a record cannot be deleted. + schema: + $ref: "../swagger.yaml#/definitions/error" "500": description: | Internal server error. Possible `error_code` attribute values: diff --git a/t/db_dependent/api/v1/record_sources.t b/t/db_dependent/api/v1/record_sources.t index 0fb37a9091..2ab1e5f1f4 100755 --- a/t/db_dependent/api/v1/record_sources.t +++ b/t/db_dependent/api/v1/record_sources.t @@ -136,7 +136,7 @@ subtest 'get() tests' => sub { subtest 'delete() tests' => sub { - plan tests => 10; + plan tests => 12; $schema->storage->txn_begin; @@ -175,6 +175,14 @@ subtest 'delete() tests' => sub { $source = $builder->build_object( { class => 'Koha::RecordSources' } ); $id = $source->id; + my $biblio = $builder->build_sample_biblio(); + my $metadata = $biblio->metadata; + $metadata->record_source_id( $source->id )->store(); + + $t->delete_ok("//$userid:$password@/api/v1/record_sources/$id")->status_is( 409, 'REST3.2.4.1' ); + + $biblio->delete(); + $t->delete_ok("//$userid:$password@/api/v1/record_sources/$id")->status_is( 204, 'REST3.2.4' ) ->content_is( q{}, 'REST3.3.4' ); -- 2.39.5