Skip to content

Commit b184859

Browse files
committed
feat: theme error page
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
1 parent b76b0bb commit b184859

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

lib/private/legacy/OC_Template.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @author Roeland Jago Douma <roeland@famdouma.nl>
2222
* @author Thomas Müller <thomas.mueller@tmit.eu>
2323
* @author Vincent Petry <vincent@nextcloud.com>
24+
* @author Richard Steinmetz <richard@steinmetz.cloud>
2425
*
2526
* @license AGPL-3.0
2627
*
@@ -38,7 +39,9 @@
3839
*
3940
*/
4041
use OC\TemplateLayout;
42+
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
4143
use OCP\AppFramework\Http\TemplateResponse;
44+
use OCP\EventDispatcher\IEventDispatcher;
4245
use OCP\Util;
4346

4447
require_once __DIR__.'/template/functions.php';
@@ -289,20 +292,44 @@ public static function printErrorPage($error_msg, $hint = '', $statusCode = 500)
289292
// If the hint is the same as the message there is no need to display it twice.
290293
$hint = '';
291294
}
295+
$errors = [['error' => $error_msg, 'hint' => $hint]];
292296

293297
http_response_code($statusCode);
294298
try {
295-
$content = new \OC_Template('', 'error', 'error', false);
296-
$errors = [['error' => $error_msg, 'hint' => $hint]];
297-
$content->assign('errors', $errors);
298-
$content->printPage();
299-
} catch (\Exception $e) {
299+
// Try rendering themed html error page
300+
$response = new TemplateResponse(
301+
'',
302+
'error',
303+
['errors' => $errors],
304+
TemplateResponse::RENDER_AS_ERROR,
305+
$statusCode,
306+
);
307+
$event = new BeforeTemplateRenderedEvent(false, $response);
308+
\OC::$server->get(IEventDispatcher::class)->dispatchTyped($event);
309+
print($response->render());
310+
} catch (\Throwable $e1) {
300311
$logger = \OC::$server->getLogger();
301-
$logger->error("$error_msg $hint", ['app' => 'core']);
302-
$logger->logException($e, ['app' => 'core']);
312+
$logger->logException($e1, [
313+
'app' => 'core',
314+
'message' => 'Rendering themed error page failed. Falling back to unthemed error page.'
315+
]);
303316

304-
header('Content-Type: text/plain; charset=utf-8');
305-
print("$error_msg $hint");
317+
try {
318+
// Try rendering unthemed html error page
319+
$content = new \OC_Template('', 'error', 'error', false);
320+
$content->assign('errors', $errors);
321+
$content->printPage();
322+
} catch (\Exception $e2) {
323+
// If nothing else works, fall back to plain text error page
324+
$logger->error("$error_msg $hint", ['app' => 'core']);
325+
$logger->logException($e2, [
326+
'app' => 'core',
327+
'message' => 'Rendering unthemed error page failed. Falling back to plain text error page.'
328+
]);
329+
330+
header('Content-Type: text/plain; charset=utf-8');
331+
print("$error_msg $hint");
332+
}
306333
}
307334
die();
308335
}

0 commit comments

Comments
 (0)