forked from Rehike/Rehike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatalHandler.php
85 lines (74 loc) · 2.35 KB
/
fatalHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* This should be completely redone
*
* love taniko
*/
register_shutdown_function("fatalHandler");
// wow this code really fucking sucks
function fatalHandler() {
global $buildFatalPage;
$e = error_get_last();
if ($e !== null && ($e['type'] == E_ERROR || $e['type'] == E_CORE_ERROR)) {
ob_end_clean(); ob_start(); // wipe output buffer and restart
try {
$buildFatalPage($e);
} catch (Throwable $err) {
try {
$buildFatalPage($e, true);
} catch (Throwable $err) {
echo 'Fatal error trying to catch fatal error trying to catch fatal error. Fuck.';
echo "<br><br>";
echo $e["message"] . " in " . $e["file"] . ":" . $e["line"] . " <b><". $e["type"] ."></b>";
}
}
ob_end_flush();
}
}
$buildFatalPage = function ($e, $simple = false) use ($yt) {
$errInfo = (object) [];
$errInfo->type = $e['type'] ?? E_CORE_ERROR;
$errInfo->file = $e['file'] ?? '(unknown file)';
$errInfo->line = $e['line'] ?? 0;
$errInfo->message = $e['message'] ?? '(no message)';
$errInfo->messagePreview = fatalPreviewify($errInfo->message);
if (!$simple) {
//$twig->addGlobal('errInfo', $errInfo);
$yt->errInfo = $errInfo;
echo Rehike\TemplateManager::render([], 'fatal');
} else {
header('Content-Type: text/html');
echo "<h1>Rehike pre-init error occurred</h1>";
echo "<h2>Here are the details:</h2>";
echo "<p>$errInfo->message</p>";
echo "<h2>Technical details</h2>";
echo "<pre>";
echo json_encode($errInfo, JSON_PRETTY_PRINT);
echo "</pre>";
}
};
function fatalPreviewify($msg) {
$response = "";
if (substr($msg, 0, 19) == "Uncaught Twig\Error") {
return simplifyTwigError($msg);
} else {
$msg = 'Fatal error: ' . $msg;
$msg = explode("Stack trace", $msg)[0];
if (strlen($msg) > 90) {
return substr($msg, 0, 87) . '...';
} else {
return $msg;
}
}
}
function simplifyTwigError($msg) {
$response = 'Fatal error (in Twig): ';
$re = '/(Uncaught Twig\\\\Error\\\\(.*?): )|( \()/';
preg_match_all($re, $msg, $matches, PREG_OFFSET_CAPTURE);
$newmsg = substr(
$msg,
strlen($matches[0][0][0]),
$matches[0][1][1] - strlen($matches[0][0][0])
);
return $response . ' ' . $newmsg;
}