From 834813bf53ec2aaf682bae9c63ce91fe54a7d778 Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Thu, 18 Sep 2025 18:52:39 -0400 Subject: [PATCH 1/2] rename phpunitnodieexception to nodieexception --- resources/lib/UnitySite.php | 6 +++--- .../{PhpUnitNoDieException.php => NoDieException.php} | 2 +- test/functional/NewUserTest.php | 1 - test/functional/ViewAsUserTest.php | 1 - test/phpunit-bootstrap.php | 4 ++-- test/unit/UnitySiteTest.php | 8 ++++---- 6 files changed, 10 insertions(+), 12 deletions(-) rename resources/lib/exceptions/{PhpUnitNoDieException.php => NoDieException.php} (53%) diff --git a/resources/lib/UnitySite.php b/resources/lib/UnitySite.php index 09d81404..31207f88 100644 --- a/resources/lib/UnitySite.php +++ b/resources/lib/UnitySite.php @@ -3,7 +3,7 @@ namespace UnityWebPortal\lib; use phpseclib3\Crypt\PublicKeyLoader; -use UnityWebPortal\lib\exceptions\PhpUnitNoDieException; +use UnityWebPortal\lib\exceptions\NoDieException; class UnitySite { @@ -11,9 +11,9 @@ public static function die($x = null, $show_user = false) { if (CONFIG["site"]["allow_die"] == false) { if (is_null($x)) { - throw new PhpUnitNoDieException(); + throw new NoDieException(); } else { - throw new PhpUnitNoDieException($x); + throw new NoDieException($x); } } else { if (!is_null($x) and $show_user) { 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'); } From 9a15f503e38b1b09a7eecf6108ce2a2de98b5947 Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Thu, 18 Sep 2025 19:06:51 -0400 Subject: [PATCH 2/2] make forbidden and badRequest prettier --- resources/lib/UnitySite.php | 59 ++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/resources/lib/UnitySite.php b/resources/lib/UnitySite.php index 31207f88..b0b6b3e4 100644 --- a/resources/lib/UnitySite.php +++ b/resources/lib/UnitySite.php @@ -7,20 +7,31 @@ 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 NoDieException(); - } else { - throw new NoDieException($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 " +

$msg

+

+ 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 " -

An internal server error has occurred.

-

- 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)