From 80d9065d943e689cf790b1bce21350568549ed7f Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Fri, 16 Jan 2026 17:15:37 -0500 Subject: [PATCH 1/2] record not found exception --- resources/autoload.php | 1 + resources/lib/exceptions/RecordNotFoundException.php | 5 +++++ test/phpunit-bootstrap.php | 1 + 3 files changed, 7 insertions(+) create mode 100644 resources/lib/exceptions/RecordNotFoundException.php diff --git a/resources/autoload.php b/resources/autoload.php index 450dc2937..cf4579d7d 100644 --- a/resources/autoload.php +++ b/resources/autoload.php @@ -34,6 +34,7 @@ require_once __DIR__ . "/lib/exceptions/EnsureException.php"; require_once __DIR__ . "/lib/exceptions/EncodingUnknownException.php"; require_once __DIR__ . "/lib/exceptions/EncodingConversionException.php"; +require_once __DIR__ . "/lib/exceptions/RecordNotFoundException.php"; require_once __DIR__ . "/lib/exceptions/UnityHTTPDMessageNotFoundException.php"; require_once __DIR__ . "/config.php"; diff --git a/resources/lib/exceptions/RecordNotFoundException.php b/resources/lib/exceptions/RecordNotFoundException.php new file mode 100644 index 000000000..ee7012bec --- /dev/null +++ b/resources/lib/exceptions/RecordNotFoundException.php @@ -0,0 +1,5 @@ + Date: Fri, 16 Jan 2026 17:30:40 -0500 Subject: [PATCH 2/2] SQL stricter getters --- resources/autoload.php | 1 + resources/lib/UnitySQL.php | 27 +++++++++++++------ .../exceptions/RecordNotUniqueException.php | 5 ++++ test/phpunit-bootstrap.php | 1 + 4 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 resources/lib/exceptions/RecordNotUniqueException.php diff --git a/resources/autoload.php b/resources/autoload.php index cf4579d7d..ed94f9698 100644 --- a/resources/autoload.php +++ b/resources/autoload.php @@ -35,6 +35,7 @@ require_once __DIR__ . "/lib/exceptions/EncodingUnknownException.php"; require_once __DIR__ . "/lib/exceptions/EncodingConversionException.php"; require_once __DIR__ . "/lib/exceptions/RecordNotFoundException.php"; +require_once __DIR__ . "/lib/exceptions/RecordNotUniqueException.php"; require_once __DIR__ . "/lib/exceptions/UnityHTTPDMessageNotFoundException.php"; require_once __DIR__ . "/config.php"; diff --git a/resources/lib/UnitySQL.php b/resources/lib/UnitySQL.php index cad3b1469..0225577ed 100644 --- a/resources/lib/UnitySQL.php +++ b/resources/lib/UnitySQL.php @@ -3,6 +3,8 @@ namespace UnityWebPortal\lib; use PDO; +use UnityWebPortal\lib\exceptions\RecordNotUniqueException; +use UnityWebPortal\lib\exceptions\RecordNotFoundException; class UnitySQL { @@ -31,6 +33,16 @@ public function getConn(): PDO return $this->conn; } + private function throwIfEmptyOrNotUnique(array $x, string $description): void + { + if (count($x) === 0) { + throw new RecordNotFoundException($description); + } + if (count($x) > 1) { + throw new RecordNotUniqueException($description); + } + } + // // requests table methods // @@ -80,12 +92,7 @@ public function getRequest(string $user, string $dest): array $stmt->bindParam(":request_for", $dest); $stmt->execute(); $result = $stmt->fetchAll(); - if (count($result) == 0) { - throw new \Exception("no such request: uid='$user' request_for='$dest'"); - } - if (count($result) > 1) { - throw new \Exception("multiple requests for uid='$user' request_for='$dest'"); - } + $this->throwIfEmptyOrNotUnique($result, "uid='$user' request_for='$dest'"); return $result[0]; } @@ -170,7 +177,9 @@ public function getNotice(string $id): array $stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_NOTICES . " WHERE id=:id"); $stmt->bindParam(":id", $id); $stmt->execute(); - return $stmt->fetchAll()[0]; + $output = $stmt->fetchAll(); + $this->throwIfEmptyOrNotUnique($output, "id='$id'"); + return $output[0]; } public function getNotices(): array @@ -194,7 +203,9 @@ public function getPage(string $id): array $stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_PAGES . " WHERE page=:id"); $stmt->bindParam(":id", $id); $stmt->execute(); - return $stmt->fetchAll()[0]; + $output = $stmt->fetchAll(); + $this->throwIfEmptyOrNotUnique($output, "id='$id'"); + return $output[0]; } public function editPage(string $id, string $content): void diff --git a/resources/lib/exceptions/RecordNotUniqueException.php b/resources/lib/exceptions/RecordNotUniqueException.php new file mode 100644 index 000000000..d1b84e8ce --- /dev/null +++ b/resources/lib/exceptions/RecordNotUniqueException.php @@ -0,0 +1,5 @@ +