diff --git a/Koha/Exceptions/REST.pm b/Koha/Exceptions/REST.pm index bbcd0fed6b..f0150503c5 100644 --- a/Koha/Exceptions/REST.pm +++ b/Koha/Exceptions/REST.pm @@ -8,6 +8,12 @@ use Exception::Class ( 'Koha::Exceptions::REST' => { isa => 'Koha::Exception', }, + 'Koha::Exceptions::REST::Public::Authentication::Required' => { + description => "This public route requires authentication", + }, + 'Koha::Exceptions::REST::Public::Unauthorized' => { + description => "Unprivileged user cannot access another user's resources", + }, 'Koha::Exceptions::REST::Query::InvalidOperator' => { description => "Invalid operator found in query", fields => ['operator'] diff --git a/Koha/REST/Plugin/Exceptions.pm b/Koha/REST/Plugin/Exceptions.pm index 5107af103d..55c9cfc2c6 100644 --- a/Koha/REST/Plugin/Exceptions.pm +++ b/Koha/REST/Plugin/Exceptions.pm @@ -68,6 +68,23 @@ sub register { } ); } + elsif ( blessed $exception + && ref($exception) eq 'Koha::Exceptions::REST::Public::Authentication::Required' ) + { + return $c->render( + status => 401, + json => { + error => $exception->error, + } + ); + } elsif ( blessed $exception && ref($exception) eq 'Koha::Exceptions::REST::Public::Unauthorized' ) { + return $c->render( + status => 403, + json => { + error => $exception->error, + } + ); + } if ( blessed $exception ) { $type = "(" . ref($exception) . ")";