Skip to content

Commit 4a448f3

Browse files
authored
Merge pull request #34581 from nextcloud/backport/34438/stable25
[stable25] return proper error code when reporting exception fails in remote.php
2 parents 62e5313 + b695d2f commit 4a448f3

1 file changed

Lines changed: 37 additions & 33 deletions

File tree

remote.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,46 @@ class RemoteException extends Exception {
5050
* @param Exception|Error $e
5151
*/
5252
function handleException($e) {
53-
$request = \OC::$server->getRequest();
54-
// in case the request content type is text/xml - we assume it's a WebDAV request
55-
$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
56-
if ($isXmlContentType === 0) {
57-
// fire up a simple server to properly process the exception
58-
$server = new Server();
59-
if (!($e instanceof RemoteException)) {
60-
// we shall not log on RemoteException
61-
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->get(LoggerInterface::class)));
62-
}
63-
$server->on('beforeMethod:*', function () use ($e) {
64-
if ($e instanceof RemoteException) {
65-
switch ($e->getCode()) {
66-
case 503:
67-
throw new ServiceUnavailable($e->getMessage());
68-
case 404:
69-
throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
70-
}
53+
try {
54+
$request = \OC::$server->getRequest();
55+
// in case the request content type is text/xml - we assume it's a WebDAV request
56+
$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
57+
if ($isXmlContentType === 0) {
58+
// fire up a simple server to properly process the exception
59+
$server = new Server();
60+
if (!($e instanceof RemoteException)) {
61+
// we shall not log on RemoteException
62+
$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->get(LoggerInterface::class)));
7163
}
72-
$class = get_class($e);
73-
$msg = $e->getMessage();
74-
throw new ServiceUnavailable("$class: $msg");
75-
});
76-
$server->exec();
77-
} else {
78-
$statusCode = 500;
79-
if ($e instanceof \OC\ServiceUnavailableException) {
80-
$statusCode = 503;
81-
}
82-
if ($e instanceof RemoteException) {
83-
// we shall not log on RemoteException
84-
OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
64+
$server->on('beforeMethod:*', function () use ($e) {
65+
if ($e instanceof RemoteException) {
66+
switch ($e->getCode()) {
67+
case 503:
68+
throw new ServiceUnavailable($e->getMessage());
69+
case 404:
70+
throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
71+
}
72+
}
73+
$class = get_class($e);
74+
$msg = $e->getMessage();
75+
throw new ServiceUnavailable("$class: $msg");
76+
});
77+
$server->exec();
8578
} else {
86-
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
87-
OC_Template::printExceptionErrorPage($e, $statusCode);
79+
$statusCode = 500;
80+
if ($e instanceof \OC\ServiceUnavailableException) {
81+
$statusCode = 503;
82+
}
83+
if ($e instanceof RemoteException) {
84+
// we shall not log on RemoteException
85+
OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
86+
} else {
87+
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
88+
OC_Template::printExceptionErrorPage($e, $statusCode);
89+
}
8890
}
91+
} catch (\Exception $e) {
92+
OC_Template::printExceptionErrorPage($e, 500);
8993
}
9094
}
9195

0 commit comments

Comments
 (0)