Skip to content

Commit 895fba4

Browse files
authored
replace shutdown function with exception handler (#336)
1 parent 9cb1f68 commit 895fba4

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

resources/init.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use UnityWebPortal\lib\UnityGithub;
1515
use UnityWebPortal\lib\UnityHTTPD;
1616

17-
register_shutdown_function(array("UnityWebPortal\lib\UnityHTTPD", "shutdown"));
18-
// shutdown function logs errors, don't want duplicate output
19-
ini_set("log_errors", false);
17+
set_exception_handler(["UnityWebPortal\lib\UnityHTTPD", "exceptionHandler"]);
2018

2119
session_start();
2220

resources/lib/UnityHTTPD.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,12 @@ public static function internalServerError($message, $error = null, $data = null
126126
self::die($message);
127127
}
128128

129-
// https://www.php.net/manual/en/function.register-shutdown-function.php
130-
public static function shutdown()
129+
// https://www.php.net/manual/en/function.set-exception-handler.php
130+
public static function exceptionHandler($e)
131131
{
132-
$e = error_get_last();
133-
if (is_null($e) || $e["type"] !== E_ERROR) {
134-
return;
135-
}
136-
// newlines are bad for error log
137-
if (!is_null($e) && array_key_exists("message", $e) && str_contains($e["message"], "\n")) {
138-
$e["message"] = explode("\n", $e["message"]);
139-
}
140-
// error_get_last is an array, not a Throwable
141-
self::internalServerError("An internal server error has occurred.", data: ["error" => $e]);
132+
ini_set("log_errors", true); // in case something goes wrong and error is not logged
133+
self::internalServerError("An internal server error has occurred.", error: $e);
134+
ini_set("log_errors", false); // error logged successfully
142135
}
143136

144137
public static function getPostData(...$keys)

0 commit comments

Comments
 (0)