Bug 38926: Make POST /biblios return 400 if AddBiblio fails

We don't have proper exceptions in the `C4::Biblio::AddBiblio` method,
but we at least know `$biblio_id` will be `undef` in the even of an
error processing the call.

This patch makes the controller handle this situation so (at least) it
is obvious that something bad happened.

To test:
1. Apply the regression tests patch
2. Run:
   $ ktd --shell
  k$ prove t/db_dependent/api/v1/biblios.t
=> FAIL: Tests fail! The endpoint returns 200 even on error!
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass! The endpoint returns a 400 with a reasonable
message!
5. Sign off :-D

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Tomás Cohen Arazi 2025-01-20 10:06:22 -03:00 committed by Katrin Fischer
parent 384b869ccd
commit a98760a994
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834
2 changed files with 19 additions and 3 deletions

View file

@ -704,9 +704,22 @@ sub add {
) if $duplicatebiblionumber;
}
my ( $biblio_id ) = AddBiblio( $record, $frameworkcode, { record_source_id => $record_source_id } );
my ($biblio_id) = C4::Biblio::AddBiblio( $record, $frameworkcode, { record_source_id => $record_source_id } );
$c->render(
if ( !$biblio_id ) {
# FIXME: AddBiblio wraps everything inside a transaction and a try/catch block
# this will need a tweak if this behavior changes
return $c->render(
status => 400,
openapi => {
error => 'Error creating record',
error_code => 'record_creation_failed',
},
);
}
return $c->render(
status => 200,
openapi => { id => $biblio_id }
);

View file

@ -29,7 +29,10 @@
"200":
description: A biblio
"400":
description: Bad request
description: |
Bad request. Possible `error_code` attribute values:
* `record_creation_failed`: An error occurred during record creation. The cause could not be determined.
schema:
$ref: "../swagger.yaml#/definitions/error"
"401":