This commit is contained in:
2024-07-29 22:06:57 +02:00
parent 5ce2215e44
commit 6a62ae58fc
22 changed files with 670 additions and 7 deletions
+39
View File
@@ -4,6 +4,9 @@ namespace Khofmann\ApiError;
use Exception;
/**
* Facade for common API errors
*/
class ApiError extends Exception
{
private function __construct($message = "", $code = 0)
@@ -11,6 +14,11 @@ class ApiError extends Exception
parent::__construct($message, $code);
}
/**
* Error for missing fields
*
* @param array $fields Array of strings denoting which fields were missing
*/
public static function missingField(array $fields): ApiError
{
return new ApiError(json_encode([
@@ -19,6 +27,11 @@ class ApiError extends Exception
]), 400);
}
/**
* Error for duplicates
*
* @param string entity Entity for which a duplicate exists
*/
public static function duplicate(string $entity): ApiError
{
return new ApiError(json_encode([
@@ -27,6 +40,11 @@ class ApiError extends Exception
]), 400);
}
/**
* Error for missing permissions
*
* @param string message Message specifics
*/
public static function notAllowed(string $message)
{
return new ApiError(json_encode([
@@ -35,6 +53,11 @@ class ApiError extends Exception
]), 401);
}
/**
* Error for missing authentication
*
* @param string $message Message specifics
*/
public static function unauthorized(string $message)
{
return new ApiError(json_encode([
@@ -43,6 +66,11 @@ class ApiError extends Exception
]), 401);
}
/**
* Error for not found
*
* @param string entity Entity for which a duplicate exists
*/
public static function notFound(string $entity)
{
return new ApiError(json_encode([
@@ -51,6 +79,11 @@ class ApiError extends Exception
]), 404);
}
/**
* Generic error
*
* @param string message Message specifics
*/
public static function failed(string $message)
{
return new ApiError(json_encode([
@@ -59,6 +92,12 @@ class ApiError extends Exception
]), 500);
}
/**
* Error for missing fields
*
* @param array $fields Array of strings denoting which fields failed
* @param array $fields Array of strings denoting why the fields failed
*/
public static function failedUpdate(array $fields, array $reasons)
{
return new ApiError(json_encode([