Skip to content

Commit 26a23c1

Browse files
committed
shudown handler for fatal errors
1 parent c8804ba commit 26a23c1

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private function initSession(): void
5959
*/
6060
public function bootstrap(): Container
6161
{
62+
\register_shutdown_function(Exception::getShutdownHandler());
6263
$env = new Environment($_SERVER);
6364
$config = $env->fetchConfig($this->configFolder, $this->environment);
6465
$config[Environment::class] = $env;

src/Exception.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,92 @@
44

55
namespace Bone;
66

7+
use Closure;
8+
79
class Exception extends \Exception
810
{
911
const SHIVER_ME_TIMBERS = 'Application Error';
1012
const LOST_AT_SEA = 'Page not found.';
1113
const GHOST_SHIP = 'Record not found.';
14+
15+
public static function getShutdownHandler(): Closure
16+
{
17+
return function () {
18+
$error = error_get_last();
19+
20+
if ($error['type'] === E_ERROR) {
21+
$split = preg_split("/Stack\strace\:\n/", $error["message"]);
22+
$split2 = preg_split("/\sin\s/", $split[0]);
23+
$message = $split2[0];
24+
$where = $split2[1];
25+
$lines = explode("\n", $split[1]);
26+
$trace = '';
27+
$row = 'odd';
28+
29+
foreach ($lines as $line) {
30+
$split = preg_split("/\s+/", $line, 2);
31+
$traceNo = $split[0];
32+
$lineInfo = $split[1];
33+
$trace .= "<tr class='$row'><td>$traceNo</td><td>$lineInfo</td></tr>";
34+
$row = $row === 'odd' ? 'even' : 'odd';
35+
}
36+
37+
if (\getenv('APPLICATION_ENV') === 'production') {
38+
$message = 'There was an error';
39+
$where = 'We apologise for the inconvenience.';
40+
$trace = '';
41+
}
42+
43+
$content = "
44+
<html>
45+
<head>
46+
<style type='text/css'>
47+
#error {
48+
background: rgb(0,212,255);
49+
background: radial-gradient(circle, rgba(0,212,255,1) 0%, rgba(4,23,62,1) 78%);
50+
display: flex;
51+
justify-content: center;
52+
font-family: Helvetica
53+
}
54+
h1, h3 {
55+
font-weight: initial;
56+
}
57+
#details {
58+
width: 75%;
59+
background-color: #eee;
60+
padding: 20px;
61+
margin: 20px;
62+
}
63+
table {
64+
border: 1px solid #999;
65+
}
66+
tr {
67+
68+
}
69+
tr.odd {
70+
background-color: #ddd;
71+
}
72+
tr.even {
73+
74+
}
75+
td {
76+
padding: 10px;
77+
}
78+
</style>
79+
</head>
80+
<body id='error'>
81+
<div id='details'>
82+
<h1>$message</h1>
83+
<h3>$where</h3>
84+
<table>
85+
$trace
86+
</table>
87+
</div>
88+
</body>
89+
90+
</html>";
91+
echo $content;
92+
}
93+
};
94+
}
1295
}

0 commit comments

Comments
 (0)