Skip to content

Commit 1047d52

Browse files
authored
Merge pull request #56170 from nextcloud/jtr/fix-public-exceptions-http-codes
fix: use specified HTTP codes during exceptions on public/remote endpoints
2 parents 4cd65b0 + a287169 commit 1047d52

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

public.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ function resolveService(string $service): string {
8989
$baseuri = OC::$WEBROOT . '/public.php/' . $service . '/';
9090
require_once $file;
9191
} catch (Exception $ex) {
92-
$status = 500;
93-
if ($ex instanceof ServiceUnavailableException) {
94-
$status = 503;
92+
if ($ex->getCode() > 0) {
93+
$status = $ex->getCode();
94+
} else {
95+
$status = $ex instanceof ServiceUnavailableException ? 503 : 500;
9596
}
9697
//show the user a detailed error page
9798
Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]);

remote.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ function handleException(Exception|Error $e): void {
5454
});
5555
$server->exec();
5656
} else {
57-
$statusCode = 500;
58-
if ($e instanceof ServiceUnavailableException) {
59-
$statusCode = 503;
60-
}
6157
if ($e instanceof RemoteException) {
6258
// we shall not log on RemoteException
6359
\OCP\Server::get(ITemplateManager::class)->printErrorPage($e->getMessage(), '', $e->getCode());
6460
} else {
61+
if ($e->getCode() > 0) {
62+
$status = $e->getCode();
63+
} else {
64+
$status = $e instanceof ServiceUnavailableException ? 503 : 500;
65+
}
6566
\OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
66-
\OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, $statusCode);
67+
\OCP\Server::get(ITemplateManager::class)->printExceptionErrorPage($e, $status);
6768
}
6869
}
6970
} catch (\Exception $e) {

0 commit comments

Comments
 (0)