Bugzilla – Attachment 109233 Details for
Bug 25504
Wrong API spec breaks plack without meaningful error
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 25504: Improve REST API spec loading errors
Bug-25504-Improve-REST-API-spec-loading-errors.patch (text/plain), 4.35 KB, created by
Tomás Cohen Arazi (tcohen)
on 2020-08-27 14:37:05 UTC
(
hide
)
Description:
Bug 25504: Improve REST API spec loading errors
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2020-08-27 14:37:05 UTC
Size:
4.35 KB
patch
obsolete
>From 41386f1c22eccc7d46d9698e965d0881c273f548 Mon Sep 17 00:00:00 2001 >From: Ere Maijala <ere.maijala@helsinki.fi> >Date: Thu, 18 Jun 2020 16:43:42 +0300 >Subject: [PATCH] Bug 25504: Improve REST API spec loading errors > >Test plan: >1. Introduce a typo in swagger.json or another spec json file. >2. Restart plack if used or try to access the REST APIs >3. Without the patch, verify that an incomplete error message and potentially lots of stack trace are logged. >4. With the patch, verify that much more meaningful error messages are logged and stack trace is omitted. >5. Fix the problem introduced in step 1 and verify that no messages are logged, or only warning about bundle is logged with Debian Stretch. >6. Repeat with a REST API plugin. > >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> >--- > Koha/REST/Plugin/PluginRoutes.pm | 4 ++- > Koha/REST/V1.pm | 61 +++++++++++++++++++------------- > 2 files changed, 39 insertions(+), 26 deletions(-) > >diff --git a/Koha/REST/Plugin/PluginRoutes.pm b/Koha/REST/Plugin/PluginRoutes.pm >index ec86c0f4d9..87bd2a6030 100644 >--- a/Koha/REST/Plugin/PluginRoutes.pm >+++ b/Koha/REST/Plugin/PluginRoutes.pm >@@ -89,7 +89,9 @@ sub inject_routes { > return $spec; > } > catch { >- warn "$_"; >+ my $error = $_; >+ my $class = ref $plugin; >+ warn "Plugin $class route injection failed: $error"; > return $spec; > }; > } >diff --git a/Koha/REST/V1.pm b/Koha/REST/V1.pm >index 84f90115b3..c490b6c3fe 100644 >--- a/Koha/REST/V1.pm >+++ b/Koha/REST/V1.pm >@@ -20,6 +20,7 @@ use Modern::Perl; > use Mojo::Base 'Mojolicious'; > > use C4::Context; >+use Carp; > use JSON::Validator::OpenAPI::Mojolicious; > use Try::Tiny; > >@@ -103,32 +104,42 @@ sub startup { > catch { > # Validation of the complete spec failed. Resort to validation one-by-one > # to catch bad ones. >- $validator->load_and_validate_schema( >- $self->home->rel_file("api/v1/swagger/swagger.json"), >- { >- allow_invalid_ref => 1, >- schema => ( $swagger_schema ) ? $swagger_schema : undef, >- } >- ); >- >- $spec = $validator->schema->data; >- $self->plugin( >- 'Koha::REST::Plugin::PluginRoutes' => { >- spec => $spec, >- validator => $validator >- } >- ) unless C4::Context->needs_install; # load only if Koha is installed > >- $self->plugin( >- OpenAPI => { >- spec => $spec, >- route => $self->routes->under('/api/v1')->to('Auth#under'), >- allow_invalid_ref => >- 1, # required by our spec because $ref directly under >- # Paths-, Parameters-, Definitions- & Info-object >- # is not allowed by the OpenAPI specification. >- } >- ); >+ # JSON::Validator uses confess, so trim call stack from the message. >+ carp "Warning: Could not load REST API spec bundle: " . ($_ =~ /\A(.*?)$/ms)[0]; >+ >+ try { >+ $validator->load_and_validate_schema( >+ $self->home->rel_file("api/v1/swagger/swagger.json"), >+ { >+ allow_invalid_ref => 1, >+ schema => ( $swagger_schema ) ? $swagger_schema : undef, >+ } >+ ); >+ >+ $spec = $validator->schema->data; >+ $self->plugin( >+ 'Koha::REST::Plugin::PluginRoutes' => { >+ spec => $spec, >+ validator => $validator >+ } >+ ) unless C4::Context->needs_install; # load only if Koha is installed >+ >+ $self->plugin( >+ OpenAPI => { >+ spec => $spec, >+ route => $self->routes->under('/api/v1')->to('Auth#under'), >+ allow_invalid_ref => >+ 1, # required by our spec because $ref directly under >+ # Paths-, Parameters-, Definitions- & Info-object >+ # is not allowed by the OpenAPI specification. >+ } >+ ); >+ } >+ catch { >+ # JSON::Validator uses confess, so trim call stack from the message. >+ croak "Could not load REST API spec: " . ($_ =~ /\A(.*?)$/ms)[0]; >+ }; > }; > > $self->plugin( 'Koha::REST::Plugin::Pagination' ); >-- >2.28.0
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 25504
:
104884
|
104894
|
106011
|
109227
|
109228
| 109233 |
109234
|
109235
|
109420