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
+61
View File
@@ -0,0 +1,61 @@
<?php
namespace Khofmann\ApiError;
use Exception;
class ApiError extends Exception
{
private function __construct($message = "", $code = 0)
{
parent::__construct($message, $code);
}
public static function missingField(array $fields): ApiError
{
return new ApiError(json_encode([
"code" => "MissingField",
"fields" => $fields,
]), 400);
}
public static function duplicate(string $entity): ApiError
{
return new ApiError(json_encode([
"code" => "Duplicate",
"entity" => $entity,
]), 400);
}
public static function unauthorized(string $message)
{
return new ApiError(json_encode([
"code" => "Unauthorized",
"message" => $message,
]), 401);
}
public static function notFound(string $entity)
{
return new ApiError(json_encode([
"code" => "NotFound",
"entity" => $entity,
]), 404);
}
public static function failed(string $message)
{
return new ApiError(json_encode([
"code" => "Failed",
"message" => $message,
]), 500);
}
public static function failedUpdate(array $fields)
{
return new ApiError(json_encode([
"code" => "FailedUpdate",
"fields" => $fields,
]), 500);
}
}
+3 -3
View File
@@ -16,17 +16,17 @@ class AdminAuth implements IMiddleware
// No token
if ($token === null) {
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
Response::response()->httpCode(401)->json(["code" => "Unauthorized", "message" => "Not Authorized"]);
}
try {
$user = User::getByToken($token);
if (!$user->getIsAdmin()) {
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
Response::response()->httpCode(401)->json(["code" => "Unauthorized", "message" => "Not Authorized"]);
}
} catch (Exception $err) {
// No user with this token exists
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
Response::response()->httpCode(401)->json(["code" => "Unauthorized", "message" => "Not Authorized"]);
}
}
}
+2 -2
View File
@@ -16,14 +16,14 @@ class Auth implements IMiddleware
// No token
if ($token === null) {
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
Response::response()->httpCode(401)->json(["code" => "Unauthorized", "message" => "Not Authorized"]);
}
try {
User::getByToken($token);
} catch (Exception $err) {
// No user with this token exists
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
Response::response()->httpCode(401)->json(["code" => "Unauthorized", "message" => "Not Authorized"]);
}
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ class OptAuth implements IMiddleware
User::getByToken($token);
} catch (Exception $err) {
// No user with this token exists
Response::response()->httpCode(401)->json(["message" => "Not Authorized"]);
Response::response()->httpCode(401)->json(["code" => "Unauthorized", "message" => "Not Authorized"]);
}
}
}
+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);
}
+7
View File
@@ -22,6 +22,13 @@ class Response
SimpleRouter::response()->json($value, $options, $dept);
}
public static function apiError(string $value, int $code): void
{
Response::response()->header('Content-Type: application/json; charset=utf-8')->httpCode($code);
echo $value;
exit(0);
}
public static function redirect(string $url, ?int $code = null): void
{
if ($code !== null) {