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> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
parent
5a87514b2e
commit
d01f5f6e8f
2 changed files with 38 additions and 25 deletions
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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
|
||||
# JSON::Validator uses confess, so trim call stack from the message.
|
||||
carp "Warning: Could not load REST API spec bundle: " . ($_ =~ /\A(.*?)$/ms)[0];
|
||||
|
||||
$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.
|
||||
}
|
||||
);
|
||||
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' );
|
||||
|
|
Loading…
Reference in a new issue