Skip to content

Commit d6c3e5c

Browse files
committed
Catch 500 error better
1 parent 95485b6 commit d6c3e5c

File tree

4 files changed

+93
-39
lines changed

4 files changed

+93
-39
lines changed

index-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
echo '<h1>Internal Error</h1>';
1616
echo '<p>Cannot create application.</p>';
1717
if ($DEBUG) {
18-
echo '<pre>'.$e->getMessage()."\n".$e->getTraceAsString().'</pre>';
18+
echo('<pre>'.\TgUtils\FormatUtils::getTraceAsString($e).'</pre>');
1919
}
2020
}
2121

src/WebApp/Error/Error500.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace WebApp\Error;
4+
5+
class Error500 extends \WebApp\Page {
6+
7+
public function __construct($app, $throwable = NULL) {
8+
parent::__construct($app);
9+
$this->throwable = $throwable;
10+
}
11+
12+
public function getTitle() {
13+
return 'page500_title';
14+
}
15+
16+
public function getMain() {
17+
header('HTTP/1.1 500 Internal Error');
18+
$rc = new \WebApp\Component\MainContent($this);
19+
new \WebApp\Component\Title($rc, 'page500_title');
20+
new \WebApp\Component\Subtitle($rc, 'page500_subtitle');
21+
$p = new \WebApp\Component\Paragraph($rc, 'page500_description');
22+
$p->addClass('small');
23+
if (($this->throwable != NULL) && ($DEBUG || ($this->app->config->has('debug') && $this->app->config->get('debug')))) {
24+
$rc->addChild('<pre>'.\TgUtils\FormatUtils::getTraceAsString($this->throwable).'</pre>');
25+
}
26+
return $rc;
27+
}
28+
}
29+

src/WebApp/Service.php

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,64 @@ public function __construct() {
1717
}
1818

1919
public function run() {
20-
// Process the request first
21-
$rc = $this->page->processRequest();
22-
$action = is_array($rc) ? $rc[0] : $rc;
23-
24-
// Render when required
25-
if ($action == 'render') {
26-
Log::debug('$_SERVER=', $_SERVER);
27-
Log::debug('Request=', $this->app->request);
28-
$this->theme->render($this->page);
29-
Session\Utils::isFreshLogin();
30-
} else if ($action == 'redirect') {
31-
// redirect
32-
$this->app->afterRequest();
33-
header('Location: '.$rc[1]);
20+
try {
21+
// Process the request first
22+
$rc = $this->page->processRequest();
23+
$action = is_array($rc) ? $rc[0] : $rc;
24+
25+
// Render when required
26+
if ($action == 'render') {
27+
Log::debug('$_SERVER=', $_SERVER);
28+
Log::debug('Request=', $this->app->request);
29+
$this->theme->render($this->page);
30+
Session\Utils::isFreshLogin();
31+
} else if ($action == 'redirect') {
32+
// redirect
33+
$this->app->afterRequest();
34+
header('Location: '.$rc[1]);
35+
}
36+
} catch (\Throwable $e) {
37+
\TgLog\Log::error('Cannot create application', $e);
38+
$page = new Error\Error500($this->app, $e);
39+
$page->processRequest();
40+
$this->theme->render($page);
3441
}
42+
3543
$this->app->afterRequest();
3644
}
3745

3846

3947
protected function createPage() {
40-
$request = $this->app->request;
41-
42-
// Check the path info
43-
$canonical = $this->app->router->getCanonicalPath();
44-
$requested = $request->originalPath;
45-
if ($requested != $canonical) {
46-
$params = $request->params;
47-
if ($params) $params = '?'.$params;
48-
header('Location: '.$canonical.$params);
49-
exit;
50-
}
51-
52-
// Try to find the correct page
53-
$page = $this->app->router->getPage();
54-
55-
// Get a specific error page from the theme
56-
if ($page == NULL) {
57-
$page = $this->theme->getErrorPage(404);
58-
}
59-
60-
// Get the default error page
61-
if ($page == NULL) {
62-
$page = new Error\Error404($this->app);
48+
try {
49+
$request = $this->app->request;
50+
51+
// Check the path info
52+
$canonical = $this->app->router->getCanonicalPath();
53+
$requested = $request->originalPath;
54+
if ($requested != $canonical) {
55+
$params = $request->params;
56+
if ($params) $params = '?'.$params;
57+
header('Location: '.$canonical.$params);
58+
exit;
59+
}
60+
61+
// Try to find the correct page
62+
$page = $this->app->router->getPage();
63+
64+
// Get a specific error page from the theme
65+
if ($page == NULL) {
66+
$page = $this->theme->getErrorPage(404);
67+
}
68+
69+
// Get the default error page
70+
if ($page == NULL) {
71+
$page = new Error\Error404($this->app);
72+
}
73+
return $page;
74+
} catch (\Throwable $e) {
75+
\TgLog\Log::error('Cannot create application', $e);
76+
return new Error\Error500($this->app, $e);
6377
}
64-
return $page;
6578
}
6679

6780
protected function createApp() {

src/i18n.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@
141141
'de' => 'Diese Seite existiert nicht oder es ist etwas Schreckliches passiert.',
142142
'en' => 'This page doesn\'t exist or something terrible happened.',
143143
),
144+
'page500_title' => array(
145+
'de' => 'Na toll!',
146+
'en' => 'Oh great!',
147+
),
148+
'page500_subtitle' => array(
149+
'de' => 'Jetzt habt Ihr die Seite kaputt gemacht.',
150+
'en' => 'You destroyed the page now.',
151+
),
152+
'page500_description' => array(
153+
'de' => 'Es ist etwas Schreckliches passiert.',
154+
'en' => 'Something terrible happened.',
155+
),
144156
'first_page_label' => array(
145157
'de' => '<i class="fas fa-step-backward"></i>',
146158
'en' => '<i class="fas fa-step-backward"></i>',

0 commit comments

Comments
 (0)