Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions resources/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
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
ini_set("log_errors", false);
set_exception_handler(["UnityWebPortal\lib\UnityHTTPD", "exceptionHandler"]);

session_start();

Expand Down
17 changes: 5 additions & 12 deletions resources/lib/UnityHTTPD.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,12 @@ 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($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]);
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); // error logged successfully
}

public static function getPostData(...$keys)
Expand Down