[Bug 35246] New: REST API: api/v1/biblios crashes with accept header application/json
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Bug ID: 35246 Summary: REST API: api/v1/biblios crashes with accept header application/json Change sponsored?: --- Product: Koha Version: master Hardware: All OS: All Status: NEW Severity: major Priority: P5 - low Component: REST API Assignee: koha-bugs@lists.koha-community.org Reporter: m.de.rooy@rijksmuseum.nl CC: tomascohen@gmail.com See also bug 33960. Found it testing there. [2023/11/03 10:33:00] [ERROR] GET /api/v1/biblios: unhandled exception (Mojo::Exception)<<Can't call method "to_api" on an undefined value at /usr/share/koha/Koha/Biblio.pm line 1433.>> => my $json_biblioitem = $self->biblioitem->to_api( $args ); Just testing /api/v1/biblios with Accept=application/json. Note that Accept=application/marc works fine. -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Marcel de Rooy <m.de.rooy@rijksmuseum.nl> changed: What |Removed |Added ---------------------------------------------------------------------------- See Also| |https://bugs.koha-community | |.org/bugzilla3/show_bug.cgi | |?id=33960 -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 David Cook <dcook@prosentient.com.au> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |dcook@prosentient.com.au --- Comment #1 from David Cook <dcook@prosentient.com.au> --- Is this still an issue? -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 --- Comment #2 from Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> --- (In reply to Marcel de Rooy from comment #0)
See also bug 33960. Found it testing there.
[2023/11/03 10:33:00] [ERROR] GET /api/v1/biblios: unhandled exception (Mojo::Exception)<<Can't call method "to_api" on an undefined value at /usr/share/koha/Koha/Biblio.pm line 1433.>> => my $json_biblioitem = $self->biblioitem->to_api( $args );
That means the DB is f*d up? Missing a biblioitems entry -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 --- Comment #3 from Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> --- I'd say the 'issue' here is the unhandled exception. We could detect the situation and raise a relevant exception, and return a meaningful error in the logs instead. -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Severity|major |minor -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |Needs Signoff Patch complexity|--- |Trivial patch -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 --- Comment #4 from Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> --- Created attachment 178001 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=178001&action=edit Bug 35246: Unit tests -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 --- Comment #5 from Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> --- Created attachment 178002 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=178002&action=edit Bug 35246: Make Koha::Biblio->to_api throw relevant exception if no biblioitem row -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Summary|REST API: api/v1/biblios |Bad data erorrs should |crashes with accept header |provide better logs for |application/json |api/v1/biblios Assignee|koha-bugs@lists.koha-commun |tomascohen@gmail.com |ity.org | -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |m.de.rooy@rijksmuseum.nl -- You are receiving this mail because: You are watching all bug changes.
DELETE FROM biblioitems WHERE biblionumber=1; \q
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #178002|0 |1 is obsolete| | --- Comment #6 from Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> --- Created attachment 178007 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=178007&action=edit Bug 35246: Make Koha::Biblio->to_api throw relevant exception if no biblioitem row This patch makes the `to_api()` method return an proper exception when the `$self->biblioitem` accessor returns `undef`. This is a bad data scenario and we could do better in terms of logging the situation. The new exception will be correctly rendered in the logs by the `unhandled_exception` Mojolicious helper and no behavior change on the API. I wasn't sure to keep the api tests I wrote. I originally added handling for the specific exception in the controller, but it become obvious it was not worth: too much code, the `GET /biblios` endpoint would need the same treatment, etc. And the current `500 Internal server error` is good enough as a response, and the logs explain the situation correctly. To test: 1. On a fresh `KTD` pick a biblio (say biblionumber=1) 2. Use your favourite REST tool (Postman?) to access the record with `Accept: application/json`. i.e. ``` GET kohadev.localhost/api/v1/biblios/1 Accept: application/json ``` => SUCCESS: All good 3. Delete the related `biblioitems` row: $ ktd --shell k$ koha-mysql kohadev 4. Repeat 2 => SUCCESS: You get a 500 error 5. Check the logs => FAIL: You get a cryptic message about accessing a method on an undefined object: ``` 2025/02/13 13:15:00] [ERROR] GET /api/v1/biblios: unhandled exception (Mojo::Exception)<<Can't call method "to_api" on an undefined value at /usr/share/koha/Koha/Biblio.pm line 1433.>> ``` 6. Apply this patch 7. Restart things: k$ restart_all 8. Repeat 2 => SUCCESS: You get a 500 error 9. Check the logs => SUCCESS: You get a more meaningful exception: ``` [2025/02/13 13:20:00] [ERROR] GET /api/v1/biblios/1: unhandled exception (Koha::Exceptions::RelatedObjectNotFound)<<Exception 'Koha::Exceptions::RelatedObjectNotFound' thrown 'The requested related object does not exist' with accessor => biblioitem, class => Koha::Biblioitem>> ``` 10. Run the tests: k$ prove t/db_dependent/Koha/Biblio.t \ t/db_dependent/api/v1/biblios.t => SUCCESS: They pass! 11. Sign off :-D -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Marcel de Rooy <m.de.rooy@rijksmuseum.nl> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Needs Signoff |Signed Off -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Marcel de Rooy <m.de.rooy@rijksmuseum.nl> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #178001|0 |1 is obsolete| | --- Comment #7 from Marcel de Rooy <m.de.rooy@rijksmuseum.nl> --- Created attachment 178018 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=178018&action=edit Bug 35246: Unit tests Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> -- You are receiving this mail because: You are watching all bug changes.
DELETE FROM biblioitems WHERE biblionumber=1; \q
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Marcel de Rooy <m.de.rooy@rijksmuseum.nl> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #178007|0 |1 is obsolete| | --- Comment #8 from Marcel de Rooy <m.de.rooy@rijksmuseum.nl> --- Created attachment 178019 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=178019&action=edit Bug 35246: Make Koha::Biblio->to_api throw relevant exception if no biblioitem row This patch makes the `to_api()` method return an proper exception when the `$self->biblioitem` accessor returns `undef`. This is a bad data scenario and we could do better in terms of logging the situation. The new exception will be correctly rendered in the logs by the `unhandled_exception` Mojolicious helper and no behavior change on the API. I wasn't sure to keep the api tests I wrote. I originally added handling for the specific exception in the controller, but it become obvious it was not worth: too much code, the `GET /biblios` endpoint would need the same treatment, etc. And the current `500 Internal server error` is good enough as a response, and the logs explain the situation correctly. To test: 1. On a fresh `KTD` pick a biblio (say biblionumber=1) 2. Use your favourite REST tool (Postman?) to access the record with `Accept: application/json`. i.e. ``` GET kohadev.localhost/api/v1/biblios/1 Accept: application/json ``` => SUCCESS: All good 3. Delete the related `biblioitems` row: $ ktd --shell k$ koha-mysql kohadev 4. Repeat 2 => SUCCESS: You get a 500 error 5. Check the logs => FAIL: You get a cryptic message about accessing a method on an undefined object: ``` 2025/02/13 13:15:00] [ERROR] GET /api/v1/biblios: unhandled exception (Mojo::Exception)<<Can't call method "to_api" on an undefined value at /usr/share/koha/Koha/Biblio.pm line 1433.>> ``` 6. Apply this patch 7. Restart things: k$ restart_all 8. Repeat 2 => SUCCESS: You get a 500 error 9. Check the logs => SUCCESS: You get a more meaningful exception: ``` [2025/02/13 13:20:00] [ERROR] GET /api/v1/biblios/1: unhandled exception (Koha::Exceptions::RelatedObjectNotFound)<<Exception 'Koha::Exceptions::RelatedObjectNotFound' thrown 'The requested related object does not exist' with accessor => biblioitem, class => Koha::Biblioitem>> ``` 10. Run the tests: k$ prove t/db_dependent/Koha/Biblio.t \ t/db_dependent/api/v1/biblios.t => SUCCESS: They pass! 11. Sign off :-D Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Nick Clemens (kidclamp) <nick@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Signed Off |Passed QA -- You are receiving this mail because: You are watching all bug changes.
DELETE FROM biblioitems WHERE biblionumber=1; \q
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Nick Clemens (kidclamp) <nick@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #178019|0 |1 is obsolete| | --- Comment #9 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 178173 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=178173&action=edit Bug 35246: Make Koha::Biblio->to_api throw relevant exception if no biblioitem row This patch makes the `to_api()` method return an proper exception when the `$self->biblioitem` accessor returns `undef`. This is a bad data scenario and we could do better in terms of logging the situation. The new exception will be correctly rendered in the logs by the `unhandled_exception` Mojolicious helper and no behavior change on the API. I wasn't sure to keep the api tests I wrote. I originally added handling for the specific exception in the controller, but it become obvious it was not worth: too much code, the `GET /biblios` endpoint would need the same treatment, etc. And the current `500 Internal server error` is good enough as a response, and the logs explain the situation correctly. To test: 1. On a fresh `KTD` pick a biblio (say biblionumber=1) 2. Use your favourite REST tool (Postman?) to access the record with `Accept: application/json`. i.e. ``` GET kohadev.localhost/api/v1/biblios/1 Accept: application/json ``` => SUCCESS: All good 3. Delete the related `biblioitems` row: $ ktd --shell k$ koha-mysql kohadev 4. Repeat 2 => SUCCESS: You get a 500 error 5. Check the logs => FAIL: You get a cryptic message about accessing a method on an undefined object: ``` 2025/02/13 13:15:00] [ERROR] GET /api/v1/biblios: unhandled exception (Mojo::Exception)<<Can't call method "to_api" on an undefined value at /usr/share/koha/Koha/Biblio.pm line 1433.>> ``` 6. Apply this patch 7. Restart things: k$ restart_all 8. Repeat 2 => SUCCESS: You get a 500 error 9. Check the logs => SUCCESS: You get a more meaningful exception: ``` [2025/02/13 13:20:00] [ERROR] GET /api/v1/biblios/1: unhandled exception (Koha::Exceptions::RelatedObjectNotFound)<<Exception 'Koha::Exceptions::RelatedObjectNotFound' thrown 'The requested related object does not exist' with accessor => biblioitem, class => Koha::Biblioitem>> ``` 10. Run the tests: k$ prove t/db_dependent/Koha/Biblio.t \ t/db_dependent/api/v1/biblios.t => SUCCESS: They pass! 11. Sign off :-D Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Nick Clemens <nick@bywatersolutions.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Katrin Fischer <katrin.fischer@bsz-bw.de> changed: What |Removed |Added ---------------------------------------------------------------------------- Version(s)| |25.05.00 released in| | Status|Passed QA |Pushed to main -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 --- Comment #10 from Katrin Fischer <katrin.fischer@bsz-bw.de> --- Pushed for 25.05! Well done everyone, thank you! -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Baptiste Wojtkowski (bwoj) <baptiste.wojtkowski@biblibre.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |baptiste.wojtkowski@biblibr | |e.com Version(s)|25.05.00 |25.05.00,24.11.08 released in| | Status|Pushed to main |Pushed to stable --- Comment #11 from Baptiste Wojtkowski (bwoj) <baptiste.wojtkowski@biblibre.com> --- Pushed into 24.11.x for 24.11.08 nice work everyone -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35246 Wainui Witika-Park <wainuiwitikapark@catalyst.net.nz> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Pushed to stable |Needs documenting --- Comment #12 from Wainui Witika-Park <wainuiwitikapark@catalyst.net.nz> --- Not backporting to 22.11.x as it wasn't backported to 24.05.x -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org