Bug 28907: Add REST exceptions for public routes auth

To test:
1. perl -c Koha/REST/Plugin/Exceptions.pm
2. perl -c Koha/Exceptions/REST.pm

More tests coming in following patches.

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Alex Buckley <alexbuckley@catalyst.net.nz>
This commit is contained in:
Lari Taskula 2024-09-18 10:21:47 +00:00 committed by Alex Buckley
parent 3371577b42
commit aa97527c37
2 changed files with 23 additions and 0 deletions

View File

@ -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']

View File

@ -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) . ")";