Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Print early exceptions in debug mode instead of plain error pages #42045

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions lib/private/legacy/OC_Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@
* @suppress PhanAccessMethodInternal
*/
public static function printExceptionErrorPage($exception, $statusCode = 503) {
$debug = false;
http_response_code($statusCode);
try {
$debug = \OC::$server->getSystemConfig()->getValue('debug', false);
$request = \OC::$server->getRequest();
$content = new \OC_Template('', 'exception', 'error', false);
$content->assign('errorClass', get_class($exception));
Expand All @@ -285,7 +287,7 @@
$content->assign('file', $exception->getFile());
$content->assign('line', $exception->getLine());
$content->assign('exception', $exception);
$content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false));
$content->assign('debugMode', $debug);
$content->assign('remoteAddr', $request->getRemoteAddress());
$content->assign('requestID', $request->getId());
$content->printPage();
Expand All @@ -296,22 +298,28 @@
$logger->logException($e, ['app' => 'core']);
} catch (Throwable $e) {
// no way to log it properly - but to avoid a white page of death we send some output
header('Content-Type: text/plain; charset=utf-8');
print("Internal Server Error\n\n");
print("The server encountered an internal error and was unable to complete your request.\n");
print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
print("More details can be found in the server log.\n");
self::printPlainErrorPage($e, $debug);

// and then throw it again to log it at least to the web server error log
throw $e;
}

header('Content-Type: text/plain; charset=utf-8');
print("Internal Server Error\n\n");
print("The server encountered an internal error and was unable to complete your request.\n");
print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
print("More details can be found in the server log.\n");
self::printPlainErrorPage($e, $debug);
}
die();
}

private static function printPlainErrorPage(\Throwable $exception, bool $debug = false) {
header('Content-Type: text/plain; charset=utf-8');
print("Internal Server Error\n\n");
print("The server encountered an internal error and was unable to complete your request.\n");
print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
print("More details can be found in the server log.\n");

if ($debug) {
print("\n");
print($exception->getMessage() . ' ' . $exception->getFile() . ' at ' . $exception->getLine() . "\n");
print($exception->getTraceAsString());
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
}
}
}
Loading