From 7d871426715a7fbc89aa4160b57f38233104adb0 Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Sat, 1 Nov 2025 07:47:04 -0400 Subject: [PATCH 1/4] replace shutdown function with exception handler --- resources/init.php | 4 ++-- resources/lib/UnityHTTPD.php | 15 +++------------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/resources/init.php b/resources/init.php index f388be53..de9d8fef 100644 --- a/resources/init.php +++ b/resources/init.php @@ -14,8 +14,8 @@ use UnityWebPortal\lib\UnityGithub; use UnityWebPortal\lib\UnityHTTPD; -register_shutdown_function(array("UnityWebPortal\lib\UnityHTTPD", "shutdown")); -// shutdown function logs errors, don't want duplicate output +set_exception_handler(["UnityWebPortal\lib\UnityHTTPD", "exceptionHandler"]); +// exception handler logs errors, don't want duplicate output ini_set("log_errors", false); session_start(); diff --git a/resources/lib/UnityHTTPD.php b/resources/lib/UnityHTTPD.php index df4948d7..7e76dce5 100644 --- a/resources/lib/UnityHTTPD.php +++ b/resources/lib/UnityHTTPD.php @@ -126,19 +126,10 @@ public static function internalServerError($message, $error = null, $data = null self::die($message); } - // https://www.php.net/manual/en/function.register-shutdown-function.php - public static function shutdown() + // https://www.php.net/manual/en/function.set-exception-handler.php + public static function exceptionHandler(\Throwable $e) { - $e = error_get_last(); - if (is_null($e) || $e["type"] !== E_ERROR) { - return; - } - // newlines are bad for error log - if (!is_null($e) && array_key_exists("message", $e) && str_contains($e["message"], "\n")) { - $e["message"] = explode("\n", $e["message"]); - } - // error_get_last is an array, not a Throwable - self::internalServerError("An internal server error has occurred.", data: ["error" => $e]); + self::internalServerError("An internal server error has occurred.", error: $e); } public static function getPostData(...$keys) From 776fe67a65069989b14f73d15ada7b693e8ad88c Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Sat, 1 Nov 2025 08:08:00 -0400 Subject: [PATCH 2/4] test --- resources/lib/UnityHTTPD.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/resources/lib/UnityHTTPD.php b/resources/lib/UnityHTTPD.php index 7e76dce5..5442004f 100644 --- a/resources/lib/UnityHTTPD.php +++ b/resources/lib/UnityHTTPD.php @@ -127,9 +127,13 @@ public static function internalServerError($message, $error = null, $data = null } // https://www.php.net/manual/en/function.set-exception-handler.php - public static function exceptionHandler(\Throwable $e) + public static function exceptionHandler($e) { - self::internalServerError("An internal server error has occurred.", error: $e); + try { + self::internalServerError("An internal server error has occurred.", error: $e); + } catch (\Throwable $e) { + error_log("exception handler failed! $e"); + } } public static function getPostData(...$keys) From 2797cf613266a48b68eb674f4d17c70445b13047 Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Sat, 1 Nov 2025 08:11:25 -0400 Subject: [PATCH 3/4] test 2 --- resources/init.php | 2 -- resources/lib/UnityHTTPD.php | 9 ++++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/resources/init.php b/resources/init.php index de9d8fef..a1203080 100644 --- a/resources/init.php +++ b/resources/init.php @@ -15,8 +15,6 @@ use UnityWebPortal\lib\UnityHTTPD; set_exception_handler(["UnityWebPortal\lib\UnityHTTPD", "exceptionHandler"]); -// exception handler logs errors, don't want duplicate output -ini_set("log_errors", false); session_start(); diff --git a/resources/lib/UnityHTTPD.php b/resources/lib/UnityHTTPD.php index 5442004f..74e52174 100644 --- a/resources/lib/UnityHTTPD.php +++ b/resources/lib/UnityHTTPD.php @@ -129,11 +129,10 @@ public static function internalServerError($message, $error = null, $data = null // https://www.php.net/manual/en/function.set-exception-handler.php public static function exceptionHandler($e) { - try { - self::internalServerError("An internal server error has occurred.", error: $e); - } catch (\Throwable $e) { - error_log("exception handler failed! $e"); - } + // if this fails for any reason log_errors will be enabled + ini_set("log_errors", true); + self::internalServerError("An internal server error has occurred.", error: $e); + ini_set("log_errors", false); } public static function getPostData(...$keys) From 565ed150ecd5ffc3d7d8a841b2580c927d211a84 Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Sat, 1 Nov 2025 08:15:37 -0400 Subject: [PATCH 4/4] comments --- resources/lib/UnityHTTPD.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/resources/lib/UnityHTTPD.php b/resources/lib/UnityHTTPD.php index 74e52174..61585a7c 100644 --- a/resources/lib/UnityHTTPD.php +++ b/resources/lib/UnityHTTPD.php @@ -129,10 +129,9 @@ public static function internalServerError($message, $error = null, $data = null // https://www.php.net/manual/en/function.set-exception-handler.php public static function exceptionHandler($e) { - // if this fails for any reason log_errors will be enabled - ini_set("log_errors", true); + ini_set("log_errors", true); // in case something goes wrong and error is not logged self::internalServerError("An internal server error has occurred.", error: $e); - ini_set("log_errors", false); + ini_set("log_errors", false); // error logged successfully } public static function getPostData(...$keys)