From b259630522a98d15d196bfc3d43fba8c7ff6bb2a Mon Sep 17 00:00:00 2001 From: Ere Maijala 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. --- 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..a360162362 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.20.1