diff --git a/resources/lib/UnitySite.php b/resources/lib/UnitySite.php index 09d81404..b0b6b3e4 100644 --- a/resources/lib/UnitySite.php +++ b/resources/lib/UnitySite.php @@ -3,24 +3,35 @@ namespace UnityWebPortal\lib; use phpseclib3\Crypt\PublicKeyLoader; -use UnityWebPortal\lib\exceptions\PhpUnitNoDieException; +use UnityWebPortal\lib\exceptions\NoDieException; class UnitySite { - public static function die($x = null, $show_user = false) + private static function die($msg) { if (CONFIG["site"]["allow_die"] == false) { - if (is_null($x)) { - throw new PhpUnitNoDieException(); - } else { - throw new PhpUnitNoDieException($x); - } + throw new NoDieException($msg); } else { - if (!is_null($x) and $show_user) { - die($x); - } else { - die(); - } + $errorid = uniqid(); + $e["unity_error_id"] = $errorid; + self::errorLog("internal server error", json_encode($e)); + echo " +
+ Please notify a Unity admin at " + . CONFIG["mail"]["support"] + . ". Error ID: $errorid. +
+ "; + // if content already printed, status code will be ignored and alert text may not be + // shown in the webpage in an obvious way, so make a popup + self::alert( + $msg + . " Please notify a Unity admin at " + . CONFIG["mail"]["support"] + . ". Error ID: $errorid." + ); + die(); } } @@ -59,16 +70,14 @@ public static function badRequest($message) { self::headerResponseCode(400, "bad request"); self::errorLog("bad request", $message); - error_clear_last(); - self::die($message); + self::die("Requested action or provided data is invalid."); } public static function forbidden($message) { self::headerResponseCode(403, "forbidden"); self::errorLog("forbidden", $message); - error_clear_last(); - self::die($message); + self::die("Permission denied."); } // https://www.php.net/manual/en/function.register-shutdown-function.php @@ -84,25 +93,7 @@ public static function shutdown() if (!headers_sent()) { self::headerResponseCode(500, "internal server error"); } - $errorid = uniqid(); - $e["unity_error_id"] = $errorid; - self::errorLog("internal server error", json_encode($e)); - echo " -- Please notify a Unity admin at " - . CONFIG["mail"]["support"] - . ". Error ID: $errorid. -
- "; - // if content already printed, status code will be ignored and alert text may not be - // shown in the webpage in an obvious way, so make a popup - self::alert( - "An internal server error has occurred. " - . "Please notify a Unity admin at " - . CONFIG["mail"]["support"] - . ". Error ID: $errorid." - ); + self::die("An internal server error has occurred."); } public static function arrayGetOrBadRequest(array $array, ...$keys) diff --git a/resources/lib/exceptions/PhpUnitNoDieException.php b/resources/lib/exceptions/NoDieException.php similarity index 53% rename from resources/lib/exceptions/PhpUnitNoDieException.php rename to resources/lib/exceptions/NoDieException.php index c8a0a0eb..49d8b2d8 100644 --- a/resources/lib/exceptions/PhpUnitNoDieException.php +++ b/resources/lib/exceptions/NoDieException.php @@ -2,6 +2,6 @@ namespace UnityWebPortal\lib\exceptions; -class PhpUnitNoDieException extends \Exception +class NoDieException extends \Exception { } diff --git a/test/functional/NewUserTest.php b/test/functional/NewUserTest.php index 0224cf24..d06ca3de 100644 --- a/test/functional/NewUserTest.php +++ b/test/functional/NewUserTest.php @@ -2,7 +2,6 @@ use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\DataProvider; -use UnityWebPortal\lib\exceptions\PhpUnitNoDieException; use UnityWebPortal\lib\UnityGroup; use UnityWebPortal\lib\UnityOrg; use UnityWebPortal\lib\UnitySQL; diff --git a/test/functional/ViewAsUserTest.php b/test/functional/ViewAsUserTest.php index 9c298586..c4a7e859 100644 --- a/test/functional/ViewAsUserTest.php +++ b/test/functional/ViewAsUserTest.php @@ -1,7 +1,6 @@ 1]; - $this->expectException(PhpUnitNoDieException::class); + $this->expectException(NoDieException::class); $this->expectExceptionMessage('["y"]'); UnitySite::arrayGetOrBadRequest($array, 'y'); } @@ -116,7 +116,7 @@ public function testArrayGetOrBadRequestThrowsOnMissingKeyFirstLevel() public function testArrayGetOrBadRequestThrowsOnMissingKeyNested() { $array = ['a' => []]; - $this->expectException(PhpUnitNoDieException::class); + $this->expectException(NoDieException::class); // Should include both levels $this->expectExceptionMessage('["a","b"]'); UnitySite::arrayGetOrBadRequest($array, 'a', 'b'); @@ -125,7 +125,7 @@ public function testArrayGetOrBadRequestThrowsOnMissingKeyNested() public function testArrayGetOrBadRequestThrowsWhenValueIsNullButKeyNotSet() { $array = ['a' => null]; - $this->expectException(PhpUnitNoDieException::class); + $this->expectException(NoDieException::class); $this->expectExceptionMessage('["a"]'); UnitySite::arrayGetOrBadRequest($array, 'a'); }