Bugzilla – Attachment 103753 Details for
Bug 25032
Generic unhandled exception handling
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 25032: Generic unhandled exception handling on API
Bug-25032-Generic-unhandled-exception-handling-on-.patch (text/plain), 4.62 KB, created by
Kyle M Hall (khall)
on 2020-04-27 11:02:18 UTC
(
hide
)
Description:
Bug 25032: Generic unhandled exception handling on API
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2020-04-27 11:02:18 UTC
Size:
4.62 KB
patch
obsolete
>From c9567f79bc985bab6d0111905188ceb342603f01 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 31 Mar 2020 18:43:02 -0300 >Subject: [PATCH] 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> >--- > Koha/REST/Plugin/Exceptions.pm | 83 ++++++++++++++++++++++++++++++++++ > Koha/REST/V1.pm | 3 ++ > 2 files changed, 86 insertions(+) > create mode 100644 Koha/REST/Plugin/Exceptions.pm > >diff --git a/Koha/REST/Plugin/Exceptions.pm b/Koha/REST/Plugin/Exceptions.pm >new file mode 100644 >index 0000000000..4539b847aa >--- /dev/null >+++ b/Koha/REST/Plugin/Exceptions.pm >@@ -0,0 +1,83 @@ >+package Koha::REST::Plugin::Exceptions; >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Scalar::Util qw( blessed ); >+use Koha::Logger; >+ >+use Mojo::Base 'Mojolicious::Plugin'; >+ >+=head1 NAME >+ >+Koha::REST::Plugin::Exceptions >+ >+=head1 API >+ >+=head2 Helper methods >+ >+=head3 unhandled_exception >+ >+ try { >+ ... >+ } >+ catch { >+ if ( know_exception ) { >+ handle_known_exception($_); >+ } >+ >+ $c->unhandled_exception($_); >+ } >+ >+Provides a generic and reusable way to throw unhandled exceptions. This way we >+can centralize the behaviour control (e.g. production vs. development environmet) >+ >+=cut >+ >+sub register { >+ my ( $self, $app ) = @_; >+ >+ $app->helper( >+ 'unhandled_exception' => sub { >+ my ( $c, $exception ) = @_; >+ >+ my $req = $c->req; >+ my $method = $req->method; >+ my $path = $req->url->to_abs->path; >+ my $type = ""; >+ >+ if ( blessed $exception ) { >+ $type = "(" . ref($exception) . ")"; >+ } >+ >+ my $message = "$method $path: unhandled exception $type\<\<$exception\>\>"; >+ >+ $c->app->log->error("$message"); >+ >+ $c->render( >+ status => 500, >+ openapi => { >+ error => >+ "Something went wrong, check Koha logs for details." >+ } >+ ); >+ } >+ >+ ); >+} >+ >+1; >diff --git a/Koha/REST/V1.pm b/Koha/REST/V1.pm >index 894457b0c8..680b52cd67 100644 >--- a/Koha/REST/V1.pm >+++ b/Koha/REST/V1.pm >@@ -40,6 +40,8 @@ Overloaded Mojolicious->startup method. It is called at application startup. > sub startup { > my $self = shift; > >+ $self->log( Koha::Logger->get({ interface => 'api' }) ); >+ > $self->hook( > before_dispatch => sub { > my $c = shift; >@@ -132,6 +134,7 @@ sub startup { > $self->plugin( 'Koha::REST::Plugin::Pagination' ); > $self->plugin( 'Koha::REST::Plugin::Query' ); > $self->plugin( 'Koha::REST::Plugin::Objects' ); >+ $self->plugin( 'Koha::REST::Plugin::Exceptions' ); > } > > 1; >-- >2.24.2 (Apple Git-127)
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 25032
:
102160
|
102161
|
102305
|
102306
|
102307
|
102523
|
102524
|
102525
|
102947
|
102971
|
102972
|
102973
|
102974
|
102975
|
103752
| 103753 |
103754
|
103755
|
103756