Skip to content

Commit

Permalink
Merge pull request #34585 from nextcloud/backport/34438/stable23
Browse files Browse the repository at this point in the history
[stable23] return proper error code when reporting exception fails in remote.php
  • Loading branch information
skjnldsv committed Oct 27, 2022
2 parents 8381bed + 0262461 commit 33689b7
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,46 @@ class RemoteException extends Exception {
* @param Exception|Error $e
*/
function handleException($e) {
$request = \OC::$server->getRequest();
// in case the request content type is text/xml - we assume it's a WebDAV request
$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
if ($isXmlContentType === 0) {
// fire up a simple server to properly process the exception
$server = new Server();
if (!($e instanceof RemoteException)) {
// we shall not log on RemoteException
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
}
$server->on('beforeMethod:*', function () use ($e) {
if ($e instanceof RemoteException) {
switch ($e->getCode()) {
case 503:
throw new ServiceUnavailable($e->getMessage());
case 404:
throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
}
try {
$request = \OC::$server->getRequest();
// in case the request content type is text/xml - we assume it's a WebDAV request
$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
if ($isXmlContentType === 0) {
// fire up a simple server to properly process the exception
$server = new Server();
if (!($e instanceof RemoteException)) {
// we shall not log on RemoteException
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
}
$class = get_class($e);
$msg = $e->getMessage();
throw new ServiceUnavailable("$class: $msg");
});
$server->exec();
} else {
$statusCode = 500;
if ($e instanceof \OC\ServiceUnavailableException) {
$statusCode = 503;
}
if ($e instanceof RemoteException) {
// we shall not log on RemoteException
OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
$server->on('beforeMethod:*', function () use ($e) {
if ($e instanceof RemoteException) {
switch ($e->getCode()) {
case 503:
throw new ServiceUnavailable($e->getMessage());
case 404:
throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
}
}
$class = get_class($e);
$msg = $e->getMessage();
throw new ServiceUnavailable("$class: $msg");
});
$server->exec();
} else {
\OC::$server->getLogger()->logException($e, ['app' => 'remote']);
OC_Template::printExceptionErrorPage($e, $statusCode);
$statusCode = 500;
if ($e instanceof \OC\ServiceUnavailableException) {
$statusCode = 503;
}
if ($e instanceof RemoteException) {
// we shall not log on RemoteException
OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
} else {
\OC::$server->getLogger()->logException($e, ['app' => 'remote']);
OC_Template::printExceptionErrorPage($e, $statusCode);
}
}
} catch (\Exception $e) {
OC_Template::printExceptionErrorPage($e, 500);
}
}

Expand Down

0 comments on commit 33689b7

Please sign in to comment.