Merge pull request #63 from ThomasRubini/error_handling

This commit is contained in:
Thomas Rubini 2023-01-24 11:03:53 +01:00 committed by GitHub
commit 0ee87628eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 103 additions and 23 deletions

View File

@ -26,8 +26,7 @@ final class ApprController
$A_appr = $O_apprModel->getApprById($I_appr_id); $A_appr = $O_apprModel->getApprById($I_appr_id);
if ($A_appr === null) { if ($A_appr === null) {
echo "404"; throw new HTTPSpecialCaseException(404);
return;
} }
if ($A_appr["AUTHOR_ID"] !== $_SESSION["ID"]) { if ($A_appr["AUTHOR_ID"] !== $_SESSION["ID"]) {

View File

@ -6,13 +6,13 @@ final class RecipeController
public function viewAction(Array $A_urlParams = null, Array $A_postParams = null) public function viewAction(Array $A_urlParams = null, Array $A_postParams = null)
{ {
if(count($A_urlParams)!=1){ if(count($A_urlParams)!=1){
return View::show("errors/404"); throw new HTTPSpecialCaseException(404);
} }
$O_recipeModel = new RecipeModel(); $O_recipeModel = new RecipeModel();
$A_returnArray = $O_recipeModel->getFullRecipeWithApprs($A_urlParams[0]); $A_returnArray = $O_recipeModel->getFullRecipeWithApprs($A_urlParams[0]);
if ($A_returnArray === null) { if ($A_returnArray === null) {
return View::show("errors/404"); throw new HTTPSpecialCaseException(404);
} }
$A_returnArray["ADMIN"] = Session::is_admin(); $A_returnArray["ADMIN"] = Session::is_admin();
@ -31,17 +31,17 @@ final class RecipeController
Session::login_or_die(); Session::login_or_die();
if(count($A_urlParams)!=1){ if(count($A_urlParams)!=1){
return View::show("errors/404"); throw new HTTPSpecialCaseException(404);
} }
$O_recipeModel = new RecipeModel(); $O_recipeModel = new RecipeModel();
$A_returnArray = $O_recipeModel->getFullRecipe($A_urlParams[0]); $A_returnArray = $O_recipeModel->getFullRecipe($A_urlParams[0]);
if ($A_returnArray === null) { if ($A_returnArray === null) {
return View::show("errors/404"); throw new HTTPSpecialCaseException(404);
} }
if ($A_returnArray["AUTHOR_ID"] !== $_SESSION["ID"]) { 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); View::show("recipe/edit", $A_returnArray);

View File

@ -94,7 +94,7 @@ final class UserController
public function defaultAction(Array $A_urlParams = null, Array $A_postParams = null) public function defaultAction(Array $A_urlParams = null, Array $A_postParams = null)
{ {
if(count($A_urlParams)!=0){ if(count($A_urlParams)!=0){
return View::show("errors/404"); throw new HTTPSpecialCaseException(404);
} }
Session::login_or_die(); Session::login_or_die();
@ -111,20 +111,25 @@ final class UserController
$O_userModel = new UserModel(); $O_userModel = new UserModel();
// TODO harmonize error handling here
if (isset($_FILES["profilPicture"])) { if (isset($_FILES["profilPicture"])) {
if ($_FILES['profilPicture']['error'] !== UPLOAD_ERR_OK) { 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']); $info = getimagesize($_FILES['profilPicture']['tmp_name']);
if ($info === false) { 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)) { 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'); $fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb');
@ -134,12 +139,16 @@ final class UserController
$S_email = $_POST["email"]; $S_email = $_POST["email"];
if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) { if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) {
$O_userModel->updateEmailByID($_SESSION["ID"], $_POST["email"]); $O_userModel->updateEmailByID($_SESSION["ID"], $_POST["email"]);
}else{
throw new HTTPSpecialCaseException(400, "invalid email");
} }
} }
if (isset($_POST["username"])) { if (isset($_POST["username"])) {
$S_username = $_POST["username"]; $S_username = $_POST["username"];
if (!empty($S_username)) { if (!empty($S_username)) {
$O_userModel->updateUsernameByID($_SESSION["ID"], $_POST["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) 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(); $O_userModel = new UserModel();
$A_user = $O_userModel->getUserByID($A_urlParams[0]); $A_user = $O_userModel->getUserByID($A_urlParams[0]);
if (!isset($A_user)) { if (!isset($A_user)) {
die(); throw new HTTPSpecialCaseException(404);
} }
header("Content-Type: image/png"); header("Content-Type: image/png");

View File

@ -55,11 +55,11 @@ final class controller
public function execute() public function execute()
{ {
if (!class_exists($this->_A_urlParts['controller'])) { if (!class_exists($this->_A_urlParts['controller'])) {
throw new ControllerException("Controller " . $this->_A_urlParts['controller'] . " is not valid."); throw new NotFoundException("Controller " . $this->_A_urlParts['controller'] . " is not valid.");
} }
if (!method_exists($this->_A_urlParts['controller'], $this->_A_urlParts['action'])) { if (!method_exists($this->_A_urlParts['controller'], $this->_A_urlParts['action'])) {
throw new ControllerException("Action " . $this->_A_urlParts['action'] . " of controller " . throw new NotFoundException("Action " . $this->_A_urlParts['action'] . " of controller " .
$this->_A_urlParts['controller'] . " is not valid."); $this->_A_urlParts['controller'] . " is not valid.");
} }

View File

@ -0,0 +1,23 @@
<?php
class HTTPSpecialCaseException extends Exception {
protected $httpCode;
protected $msg;
public function __construct($httpCode, $msg = "")
{
parent::__construct();
$this->httpCode = $httpCode;
$this->msg = $msg;
}
public function getHTTPCode(){
return $this->httpCode;
}
public function getMsg(){
return $this->msg;
}
}

View File

@ -0,0 +1,3 @@
<?php
class NotFoundException extends Exception {}

View File

@ -17,7 +17,7 @@ final class Model
try{ try{
self::$conn = new PDO($PDO_URI, $_ENV["DB_USERNAME"], $_ENV["DB_PASSWORD"]); self::$conn = new PDO($PDO_URI, $_ENV["DB_USERNAME"], $_ENV["DB_PASSWORD"]);
}catch(PDOException $e){ }catch(PDOException $e){
die("Connection to the database failed"); throw new HTTPSpecialCaseException(500, "Connection to the database failed");
} }
} }
} }

View File

@ -9,13 +9,13 @@ final class Utils
public static function getOrDie($DICT, $key) public static function getOrDie($DICT, $key)
{ {
if (isset($DICT[$key])) return $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) public static function intOrDie($data)
{ {
if (is_numeric($data)) return (int) $data; if (is_numeric($data)) return (int) $data;
else die("Not an int"); else throw new HTTPSpecialCaseException(400, "Not an int");
} }
} }

View File

@ -37,6 +37,7 @@ final class RecipeModel
public function getFullRecipeWithApprs($I_id) public function getFullRecipeWithApprs($I_id)
{ {
$A_recipe = self::getFullRecipe($I_id); $A_recipe = self::getFullRecipe($I_id);
if ($A_recipe === null)return null;
$O_apprModel = new ApprModel(); $O_apprModel = new ApprModel();

View File

@ -61,7 +61,7 @@ final class Session
{ {
if (!self::is_login()) { if (!self::is_login()) {
header("Location: /user/login?return_uri=".$_SERVER["REQUEST_URI"]); 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(); Session::login_or_die();
if (!self::is_admin()) { if (!self::is_admin()) {
header("Location: /"); throw new HTTPSpecialCaseException(403);
die();
} }
} }

8
Views/errors/400.php Normal file
View File

@ -0,0 +1,8 @@
<h1>Error 400</h1>
<h2>Erreur du client 😥</h2>
<?php
if (isset($A_view)) {
echo "<p> message d'erreur: $A_view </p>";
}
?>
<a href="/">Retourner à l'accueil<a>

3
Views/errors/403.php Normal file
View File

@ -0,0 +1,3 @@
<h1>Error 403</h1>
<h2>Vous n'avez pas l'autorisation d'accéder à cette page 😥</h2>
<a href="/">Retourner à l'accueil<a>

8
Views/errors/500.php Normal file
View File

@ -0,0 +1,8 @@
<h1>Error 500</h1>
<h2>Erreur interne du serveur 😥</h2>
<?php
if (isset($A_view)) {
echo "<p> message d'erreur: $A_view </p>";
}
?>
<a href="/">Retourner à l'accueil<a>

View File

@ -16,6 +16,10 @@
View::openBuffer(); View::openBuffer();
$ret = Utils::RETURN_HTML;
$I_err_httpCode = null;
$S_err_msg = null;
try try
{ {
$O_controller = new Controller($S_url, $A_postParams, $A_getParams); $O_controller = new Controller($S_url, $A_postParams, $A_getParams);
@ -23,7 +27,30 @@
} }
catch (ControleurException $O_exception) catch (ControleurException $O_exception)
{ {
echo ('An error occured: ' . $O_exception->getMessage()); View::openBuffer();
$I_err_httpCode = 500;
$S_err_msg = $O_exception->getMsg();
}
catch (NotFoundException $O_exception)
{
View::openBuffer();
$I_err_httpCode = 404;
}
catch (HTTPSpecialCaseException $O_exception)
{
View::openBuffer();
$I_err_httpCode = $O_exception->getHTTPCode();
$S_err_msg = $O_exception->getMsg();
}
if ($I_err_httpCode !== null) {
http_response_code($I_err_httpCode);
if($I_err_httpCode === 500 || $I_err_httpCode === 400) {
header_remove("Location");
}
View::show("errors/".$I_err_httpCode, $S_err_msg);
} }