Bug 37513: Handle Koha::Exceptions::Object::FKConstraintDeletio in controller

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Tomás Cohen Arazi 2024-08-19 11:16:00 -03:00 committed by Katrin Fischer
parent 5321e0fb09
commit 9b0196b7b6
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834
3 changed files with 27 additions and 2 deletions

View file

@ -23,6 +23,7 @@ use Mojo::Base 'Mojolicious::Controller';
use Koha::RecordSources;
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($_);
};
}

View file

@ -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:

View file

@ -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' );