|
21 | 21 | * @author Roeland Jago Douma <roeland@famdouma.nl> |
22 | 22 | * @author Thomas Müller <thomas.mueller@tmit.eu> |
23 | 23 | * @author Vincent Petry <vincent@nextcloud.com> |
| 24 | + * @author Richard Steinmetz <richard@steinmetz.cloud> |
24 | 25 | * |
25 | 26 | * @license AGPL-3.0 |
26 | 27 | * |
|
38 | 39 | * |
39 | 40 | */ |
40 | 41 | use OC\TemplateLayout; |
| 42 | +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; |
41 | 43 | use OCP\AppFramework\Http\TemplateResponse; |
| 44 | +use OCP\EventDispatcher\IEventDispatcher; |
42 | 45 | use OCP\Util; |
43 | 46 |
|
44 | 47 | require_once __DIR__.'/template/functions.php'; |
@@ -289,20 +292,44 @@ public static function printErrorPage($error_msg, $hint = '', $statusCode = 500) |
289 | 292 | // If the hint is the same as the message there is no need to display it twice. |
290 | 293 | $hint = ''; |
291 | 294 | } |
| 295 | + $errors = [['error' => $error_msg, 'hint' => $hint]]; |
292 | 296 |
|
293 | 297 | http_response_code($statusCode); |
294 | 298 | 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) { |
300 | 311 | $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 | + ]); |
303 | 316 |
|
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 | + } |
306 | 333 | } |
307 | 334 | die(); |
308 | 335 | } |
|
0 commit comments