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: theme error page #39122

Merged
merged 1 commit into from
Feb 24, 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
45 changes: 36 additions & 9 deletions lib/private/legacy/OC_Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <vincent@nextcloud.com>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license AGPL-3.0
*
Expand All @@ -38,7 +39,9 @@
*
*/
use OC\TemplateLayout;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;

require_once __DIR__.'/template/functions.php';

Expand Down Expand Up @@ -249,20 +252,44 @@ public static function printErrorPage($error_msg, $hint = '', $statusCode = 500)
// If the hint is the same as the message there is no need to display it twice.
$hint = '';
}
$errors = [['error' => $error_msg, 'hint' => $hint]];

http_response_code($statusCode);
try {
$content = new \OC_Template('', 'error', 'error', false);
$errors = [['error' => $error_msg, 'hint' => $hint]];
$content->assign('errors', $errors);
$content->printPage();
} catch (\Exception $e) {
// Try rendering themed html error page
$response = new TemplateResponse(
'',
'error',
['errors' => $errors],
TemplateResponse::RENDER_AS_ERROR,
$statusCode,
);
$event = new BeforeTemplateRenderedEvent(false, $response);
\OC::$server->get(IEventDispatcher::class)->dispatchTyped($event);
print($response->render());
} catch (\Throwable $e1) {
$logger = \OC::$server->getLogger();
$logger->error("$error_msg $hint", ['app' => 'core']);
$logger->logException($e, ['app' => 'core']);
$logger->logException($e1, [
'app' => 'core',
'message' => 'Rendering themed error page failed. Falling back to unthemed error page.'
]);

header('Content-Type: text/plain; charset=utf-8');
print("$error_msg $hint");
try {
// Try rendering unthemed html error page
$content = new \OC_Template('', 'error', 'error', false);
$content->assign('errors', $errors);
$content->printPage();
} catch (\Exception $e2) {
// If nothing else works, fall back to plain text error page
$logger->error("$error_msg $hint", ['app' => 'core']);
$logger->logException($e2, [
'app' => 'core',
'message' => 'Rendering unthemed error page failed. Falling back to plain text error page.'
]);

header('Content-Type: text/plain; charset=utf-8');
print("$error_msg $hint");
}
}
die();
}
Expand Down
Loading