Skip to content

Commit 909e261

Browse files
committed
use message system in exception handler
1 parent bde4033 commit 909e261

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

resources/lib/UnityHTTPD.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ public static function redirect(?string $dest = null): never
4343
self::die();
4444
}
4545

46+
public static function logThrowableAndMessageUser(
47+
\Throwable $error,
48+
string $log_title,
49+
string $log_message,
50+
string $user_message_title,
51+
string $user_message_body,
52+
) {
53+
$errorid = spl_object_hash($error);
54+
$data = [];
55+
self::errorLog($log_title, $log_message, error: $error, errorid: $errorid, data: $data);
56+
if (strlen($user_message_body) == 0) {
57+
$user_message_body = "error ID: $errorid";
58+
} else {
59+
$user_message_body .= " error ID: $errorid";
60+
}
61+
self::messageError($user_message_title, $user_message_body);
62+
}
63+
4664
// $data must be JSON serializable
4765
public static function errorLog(
4866
string $title,
@@ -72,6 +90,7 @@ public static function errorLog(
7290
}
7391
$output["REMOTE_USER"] = $_SERVER["REMOTE_USER"] ?? null;
7492
$output["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"] ?? null;
93+
$output["_REQUEST"] = $_REQUEST;
7594
if (!is_null($errorid)) {
7695
$output["errorid"] = $errorid;
7796
}
@@ -136,27 +155,17 @@ public static function forbidden(
136155
self::die($message);
137156
}
138157

139-
public static function internalServerError(
140-
string $message,
141-
?\Throwable $error = null,
142-
?array $data = null,
143-
): never {
144-
$errorid = uniqid();
145-
self::errorToUser("An internal server error has occurred.", 500, $errorid);
146-
self::errorLog("internal server error", $message, $errorid, $error, $data);
147-
if (!is_null($error) && ini_get("display_errors") && ini_get("html_errors")) {
148-
echo "<table>";
149-
echo $error->xdebug_message;
150-
echo "</table>";
151-
}
152-
self::die($message);
153-
}
154-
155158
// https://www.php.net/manual/en/function.set-exception-handler.php
156159
public static function exceptionHandler(\Throwable $e): void
157160
{
158-
ini_set("log_errors", true); // in case something goes wrong and error is not logged
159-
self::internalServerError("An internal server error has occurred.", error: $e);
161+
self::logThrowableAndMessageUser(
162+
$e,
163+
"uncaught exception",
164+
strval($e),
165+
"An internal server error has occurred.",
166+
"",
167+
);
168+
self::redirect();
160169
}
161170

162171
public static function errorHandler(int $severity, string $message, string $file, int $line)

0 commit comments

Comments
 (0)