From 66d4a89bdb965364c9243c700988a71766d39534 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Tue, 24 Jan 2023 09:49:07 +0100 Subject: [PATCH] Use error handling system in all controllers --- Controllers/ApprController.php | 3 +-- Controllers/RecipeController.php | 10 +++++----- Controllers/UserController.php | 23 ++++++++++++++++------- Kernel/Utils.php | 4 ++-- Modules/Session/Session.php | 5 ++--- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Controllers/ApprController.php b/Controllers/ApprController.php index 626e8fc..7eadc18 100644 --- a/Controllers/ApprController.php +++ b/Controllers/ApprController.php @@ -26,8 +26,7 @@ final class ApprController $A_appr = $O_apprModel->getApprById($I_appr_id); if ($A_appr === null) { - echo "404"; - return; + throw new HTTPSpecialCaseException(404); } if ($A_appr["AUTHOR_ID"] !== $_SESSION["ID"]) { diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 985b14a..de875d1 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -6,13 +6,13 @@ final class RecipeController public function viewAction(Array $A_urlParams = null, Array $A_postParams = null) { if(count($A_urlParams)!=1){ - return View::show("errors/404"); + throw new HTTPSpecialCaseException(404); } $O_recipeModel = new RecipeModel(); $A_returnArray = $O_recipeModel->getFullRecipeWithApprs($A_urlParams[0]); if ($A_returnArray === null) { - return View::show("errors/404"); + throw new HTTPSpecialCaseException(404); } $A_returnArray["ADMIN"] = Session::is_admin(); @@ -31,17 +31,17 @@ final class RecipeController Session::login_or_die(); if(count($A_urlParams)!=1){ - return View::show("errors/404"); + throw new HTTPSpecialCaseException(404); } $O_recipeModel = new RecipeModel(); $A_returnArray = $O_recipeModel->getFullRecipe($A_urlParams[0]); if ($A_returnArray === null) { - return View::show("errors/404"); + throw new HTTPSpecialCaseException(404); } if ($A_returnArray["AUTHOR_ID"] !== $_SESSION["ID"]) { - die("You are not the owner of this recipe"); + throw new HTTPSpecialCaseException(400, "You are not the owner of this recipe"); } View::show("recipe/edit", $A_returnArray); diff --git a/Controllers/UserController.php b/Controllers/UserController.php index 8a08beb..3bf0950 100644 --- a/Controllers/UserController.php +++ b/Controllers/UserController.php @@ -94,7 +94,7 @@ final class UserController public function defaultAction(Array $A_urlParams = null, Array $A_postParams = null) { if(count($A_urlParams)!=0){ - return View::show("errors/404"); + throw new HTTPSpecialCaseException(404); } Session::login_or_die(); @@ -111,20 +111,25 @@ final class UserController $O_userModel = new UserModel(); - // TODO harmonize error handling here if (isset($_FILES["profilPicture"])) { if ($_FILES['profilPicture']['error'] !== UPLOAD_ERR_OK) { - die("Upload failed with error code " . $_FILES['profilPicture']['error']); + throw new HTTPSpecialCaseException( + 400, + "Upload failed with error code " . $_FILES['profilPicture']['error'] + ); } $info = getimagesize($_FILES['profilPicture']['tmp_name']); if ($info === false) { - die("Unable to determine image type of uploaded file"); + throw new HTTPSpecialCaseException( + 400, + "Unable to determine image type of uploaded file" + ); } if (($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) { - die("Not a jpeg/png"); + throw new HTTPSpecialCaseException(400, "Not a jpeg/png"); } $fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb'); @@ -134,12 +139,16 @@ final class UserController $S_email = $_POST["email"]; if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) { $O_userModel->updateEmailByID($_SESSION["ID"], $_POST["email"]); + }else{ + throw new HTTPSpecialCaseException(400, "invalid email"); } } if (isset($_POST["username"])) { $S_username = $_POST["username"]; if (!empty($S_username)) { $O_userModel->updateUsernameByID($_SESSION["ID"], $_POST["username"]); + }else{ + throw new HTTPSpecialCaseException(400, "invalid username"); } } @@ -183,13 +192,13 @@ final class UserController public function profilePicAction(Array $A_urlParams = null, Array $A_postParams = null) { - if (count($A_urlParams) !== 1 ) die(); + if (count($A_urlParams) !== 1 ) throw new HTTPSpecialCaseException(404); $O_userModel = new UserModel(); $A_user = $O_userModel->getUserByID($A_urlParams[0]); if (!isset($A_user)) { - die(); + throw new HTTPSpecialCaseException(404); } header("Content-Type: image/png"); diff --git a/Kernel/Utils.php b/Kernel/Utils.php index 71d71aa..af510f0 100644 --- a/Kernel/Utils.php +++ b/Kernel/Utils.php @@ -9,13 +9,13 @@ final class Utils public static function getOrDie($DICT, $key) { if (isset($DICT[$key])) return $DICT[$key]; - else die("Key $key not present"); + else throw new HTTPSpecialCaseException(400, "Key $key not present"); } public static function intOrDie($data) { if (is_numeric($data)) return (int) $data; - else die("Not an int"); + else throw new HTTPSpecialCaseException(400, "Not an int"); } } diff --git a/Modules/Session/Session.php b/Modules/Session/Session.php index 828e00b..bf49959 100644 --- a/Modules/Session/Session.php +++ b/Modules/Session/Session.php @@ -61,7 +61,7 @@ final class Session { if (!self::is_login()) { header("Location: /user/login?return_uri=".$_SERVER["REQUEST_URI"]); - die(); + throw new HTTPSpecialCaseException(403); } } @@ -76,8 +76,7 @@ final class Session Session::login_or_die(); if (!self::is_admin()) { - header("Location: /"); - die(); + throw new HTTPSpecialCaseException(403); } }