Bug 33053: Handle invalid biblio_id more robustly

This patch addresses the fact the invalid FK error might differ under
some circumstances. We could try to catch the exception adding another
case, but I think this pattern is cleaner and the authors didn't provide
a fix.

We can discuss it later, as this controller class has several things to
review.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomás Cohen Arazi 2023-05-11 16:54:26 -03:00 committed by Martin Renvoize
parent 7324b403ba
commit 566117b2e7
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F

View file

@ -105,9 +105,16 @@ sub add {
my $c = shift->openapi->valid_input or return;
return try {
my $item_group_data = $c->validation->param('body');
my $biblio = Koha::Biblios->find( $c->param('biblio_id') );
return $c->render(
status => 404,
openapi => { error => 'Object not found' }
) unless $biblio;
my $item_group_data = $c->req->json;
# biblio_id comes from the path
$item_group_data->{biblio_id} = $c->validation->param('biblio_id');
$item_group_data->{biblio_id} = $biblio->id;
my $item_group = Koha::Biblio::ItemGroup->new_from_api($item_group_data);
$item_group->store->discard_changes();