Better Errors
This commit is contained in:
+15
-16
@@ -7,18 +7,20 @@ use Khofmann\Models\User\User as MUser;
|
||||
use Khofmann\Input\Input;
|
||||
use Khofmann\Response\Response;
|
||||
use Khofmann\Api\Api;
|
||||
use Khofmann\ApiError\ApiError;
|
||||
use Khofmann\Request\Request;
|
||||
|
||||
class User extends Api
|
||||
{
|
||||
public function get($id): void
|
||||
{
|
||||
// Try and get a user, 404 if not found.
|
||||
try {
|
||||
Response::json(MUser::getByID($id));
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "NotFound":
|
||||
throw new Exception("User not found", 404);
|
||||
throw ApiError::notFound("user");
|
||||
default:
|
||||
throw $err;
|
||||
}
|
||||
@@ -27,23 +29,21 @@ class User extends Api
|
||||
|
||||
public function patch($id): void
|
||||
{
|
||||
// Fetch all inputs.
|
||||
$username = Input::patch("username");
|
||||
$password = Input::patch("password");
|
||||
$image = Input::file("image");
|
||||
|
||||
// Try and update user.
|
||||
// Throw errors according to situation.
|
||||
try {
|
||||
Response::json(MUser::getByID($id)->update($username, $password, $image));
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "NotFound":
|
||||
throw new Exception("User not found", 404);
|
||||
case "FailedUsername":
|
||||
throw new Exception("Failed to update username", 500);
|
||||
case "FailedPassword":
|
||||
throw new Exception("Failed to update password", 500);
|
||||
case "FailedImage":
|
||||
throw new Exception("Failed to update image", 500);
|
||||
throw ApiError::notFound("user");
|
||||
default:
|
||||
// Due to how the failed field is handled, it's ApiError is inside the models update
|
||||
throw $err;
|
||||
}
|
||||
}
|
||||
@@ -51,24 +51,22 @@ class User extends Api
|
||||
|
||||
public function patchSelf(): void
|
||||
{
|
||||
// Fetch all inputs.
|
||||
$token = Request::token();
|
||||
$username = Input::patch("username");
|
||||
$password = Input::patch("password");
|
||||
$image = Input::file("image");
|
||||
|
||||
// Try and update user.
|
||||
// Throw errors according to situation.
|
||||
try {
|
||||
Response::json(MUser::getByToken($token)->update($username, $password, $image));
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "NotFound":
|
||||
throw new Exception("User not found", 404);
|
||||
case "FailedUsername":
|
||||
throw new Exception("Failed to update username", 500);
|
||||
case "FailedPassword":
|
||||
throw new Exception("Failed to update password", 500);
|
||||
case "FailedImage":
|
||||
throw new Exception("Failed to update image", 500);
|
||||
throw ApiError::notFound("user");
|
||||
default:
|
||||
// Due to how the failed field is handled, it's ApiError is inside the models update
|
||||
throw $err;
|
||||
}
|
||||
}
|
||||
@@ -76,12 +74,13 @@ class User extends Api
|
||||
|
||||
public function delete($id): void
|
||||
{
|
||||
// Try to delete user, 404 if not found.
|
||||
try {
|
||||
Response::json(MUser::getByID($id)->delete());
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "NotFound":
|
||||
throw new Exception("User not found", 404);
|
||||
throw ApiError::notFound("user");
|
||||
default:
|
||||
throw $err;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user