Skip to content

Commit

Permalink
Merge pull request #37458 from Fenn-CS/fix/37424/better-501-error-log…
Browse files Browse the repository at this point in the history
…ging

Avoid db connections when logging db connection errors
  • Loading branch information
Fenn-CS committed May 24, 2023
2 parents 4811a02 + d0fc159 commit 4220442
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
26 changes: 21 additions & 5 deletions index.php
Expand Up @@ -29,13 +29,17 @@
*
*/
require_once __DIR__ . '/lib/versioncheck.php';
use Psr\Log\LoggerInterface;

try {
require_once __DIR__ . '/lib/base.php';

OC::handleRequest();
} catch (\OC\ServiceUnavailableException $ex) {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
'app' => 'index',
'exception' => $ex,
]);

//show the user a detailed error page
OC_Template::printExceptionErrorPage($ex, 503);
Expand All @@ -44,8 +48,14 @@
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503);
} catch (Exception $ex2) {
try {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->getLogger()->logException($ex2, ['app' => 'index']);
\OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
'app' => 'index',
'exception' => $ex,
]);
\OC::$server->get(LoggerInterface::class)->error($ex2->getMessage(), [
'app' => 'index',
'exception' => $ex2,
]);
} catch (Throwable $e) {
// no way to log it properly - but to avoid a white page of death we try harder and ignore this one here
}
Expand All @@ -68,13 +78,19 @@
}
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
} catch (Exception $ex) {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
'app' => 'index',
'exception' => $ex,
]);

//show the user a detailed error page
OC_Template::printExceptionErrorPage($ex, 500);
} catch (Error $ex) {
try {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
'app' => 'index',
'exception' => $ex,
]);
} catch (Error $e) {
http_response_code(500);
header('Content-Type: text/plain; charset=utf-8');
Expand Down
8 changes: 7 additions & 1 deletion lib/private/Server.php
Expand Up @@ -730,7 +730,13 @@ public function __construct($webRoot, \OC\Config $config) {

if ($config->getSystemValueBool('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
if (!$config->getSystemValueBool('log_query')) {
$v = \OC_App::getAppVersions();
try {
$v = \OC_App::getAppVersions();
} catch (\Doctrine\DBAL\Exception $e) {
// Database service probably unavailable
// Probably related to https://github.com/nextcloud/server/issues/37424
return $arrayCacheFactory;
}
} else {
// If the log_query is enabled, we can not get the app versions
// as that does a query, which will be logged and the logging
Expand Down

0 comments on commit 4220442

Please sign in to comment.