Better Errors

This commit is contained in:
2024-07-23 01:12:05 +02:00
parent 30849019af
commit 85d20e034a
18 changed files with 567 additions and 390 deletions
+6 -3
View File
@@ -7,6 +7,7 @@ use DateTime;
use Exception;
use Khofmann\Models\User\User;
use JsonSerializable;
use Khofmann\ApiError\ApiError;
use Khofmann\Database\Database;
use PDO;
@@ -113,14 +114,16 @@ class Post implements JsonSerializable
{
$db = Database::getInstance();
$error = false;
if (!empty($content)) {
$stmt = $db->prepare("UPDATE egb_gaestebuch SET beitrag = :CON WHERE id = :ID");
$stmt->bindValue(":CON", $content);
$stmt->bindValue(":ID", $this->id);
$error = !$stmt->execute();
try {
if (!$stmt->execute()) throw ApiError::failedUpdate(["content"]);
} catch (Exception $e) {
throw ApiError::failedUpdate(["content"]);
}
}
if ($error) throw new Exception("FailedContent");
return Post::getByID($this->id);
}
+22 -11
View File
@@ -8,6 +8,7 @@ use DateTime;
use Khofmann\Database\Database;
use Config\Config;
use JsonSerializable;
use Khofmann\ApiError\ApiError;
use Khofmann\GUID\GUID;
use PDOException;
@@ -192,11 +193,11 @@ class User implements JsonSerializable
$stmt->bindValue(":EMA", $email);
$stmt->bindValue(":COD", $guid);
$user = User::getByID($db->lastInsertId());
try {
$stmt->execute();
$user = User::getByID($db->lastInsertId());
mail(
$email,
"Account activation GuestBookDB",
@@ -211,7 +212,7 @@ class User implements JsonSerializable
}
}
public static function confirm(string $confirmCode): bool
public static function confirm(string $confirmCode): User
{
$db = Database::getInstance();
$user = User::getByConfirmCode($confirmCode);
@@ -225,7 +226,7 @@ class User implements JsonSerializable
WHERE id = :UID"
);
$stmt->bindValue(":UID", $user->getID());
return $stmt->execute();
return User::getByID($user->getID());
}
public static function list(int $page, int $limit)
@@ -273,22 +274,28 @@ class User implements JsonSerializable
{
$db = Database::getInstance();
$error = false;
$failed = [];
if (!empty($username)) {
$stmt = $db->prepare("UPDATE egb_benutzer SET benutzer = :USR WHERE id = :ID");
$stmt->bindValue(":USR", $username);
$stmt->bindValue(":ID", $this->id);
$error = !$stmt->execute();
try {
if (!$stmt->execute()) array_push($failed, "username");
} catch (Exception $e) {
array_push($failed, "username");
}
}
if ($error) throw new Exception("FailedUsername");
if (!empty($password)) {
$stmt = $db->prepare("UPDATE egb_benutzer SET passwort = :PAS WHERE id = :ID");
$stmt->bindValue(":PAS", password_hash($password, PASSWORD_DEFAULT));
$stmt->bindValue(":ID", $this->id);
$error = !$stmt->execute();
try {
if (!$stmt->execute()) array_push($failed, "password");
} catch (Exception $e) {
array_push($failed, "password");
}
}
if ($error) throw new Exception("FailedPassword");
if (!empty($image)) {
$destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
@@ -297,9 +304,13 @@ class User implements JsonSerializable
$stmt = $db->prepare("UPDATE egb_benutzer SET image = :IMG WHERE id = :ID");
$stmt->bindValue(":IMG", $destinationFilename);
$stmt->bindValue(":ID", $this->id);
$error = !$stmt->execute();
try {
if (!$stmt->execute()) array_push($failed, "image");
} catch (Exception $e) {
array_push($failed, "image");
}
}
if ($error) throw new Exception("FailedImage");
if (count($failed) > 0) throw ApiError::failedUpdate($failed);
return User::getByID($this->id);
}