Bug 25032: Generic unhandled exception handling on API
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 31 Mar 2020 21:43:02 +0000 (18:43 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 29 Apr 2020 15:24:07 +0000 (16:24 +0100)
commit665d651e95145ed38e2a5c796c70bf7b43f48042
tree3b19d518bc028a70791f4a34b273fca34aca346c
parent9d879dd1eedd99791e9c7e8acb9f5e6dbea81434
Bug 25032: Generic unhandled exception handling on API

This patch adds Koha::Logger as the default logger for the API, and
introduces a new helper plugin that takes care of handling the unhandled
exceptions. Basically, with this we would write something like this in
our controller methods:

    try {
        ...
    }
    catch {
        if ( know_exception ) {
            handle_known_exception($_);
        }

        $c->unhandled_exception($_);
    }

Without this, we end up adding more and more handling 'just in case'.

To test:
1. Edit the Koha/REST/V1/Cities.pm 'list' method adding
   die("Nada"); before the render step.
2. Restart plack and try the endpoint
=> FAIL: A generic error is displayed, and no traces of the original
problem are found on the logs.
3. Apply this patches, make sure your instance's log4perl has the
   introduced lines for API with the right path.
4. Repeat 2
=> SUCCESS: The message is still generic, but you see something is
logged in /var/log/koha/kohadev/api-error.log
5. Change die("Nada"); for a real exception like:

    use Koha::Exceptions;
    Koha::Exceptions::DuplicateObject->throw("Nada");
6. Repeat 2.
=> SUCCESS: The message is generic, but a meaningful text is added to
the logs.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Koha/REST/Plugin/Exceptions.pm [new file with mode: 0644]
Koha/REST/V1.pm