first commit
This commit is contained in:
14
app/Exceptions/Entities/AppAlreadyInstalledException.php
Normal file
14
app/Exceptions/Entities/AppAlreadyInstalledException.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class AppAlreadyInstalledException extends HttpException
|
||||
{
|
||||
protected $status = 400;
|
||||
|
||||
protected $errorCode = 'app.installation';
|
||||
|
||||
protected $message = 'App has been already installed';
|
||||
}
|
||||
172
app/Exceptions/Entities/AuthorizationException.php
Normal file
172
app/Exceptions/Entities/AuthorizationException.php
Normal file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class AuthorizationException extends HttpException
|
||||
{
|
||||
/**
|
||||
* @apiDefine 400Error
|
||||
* @apiError (Error 4xx) {String} message Message from server
|
||||
* @apiError (Error 4xx) {Boolean} success Indicates erroneous response when `FALSE`
|
||||
* @apiError (Error 4xx) {String} error_type Error type
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @apiDefine UnauthorizedError
|
||||
* @apiErrorExample {json} Unauthorized
|
||||
* HTTP/1.1 401 Unauthorized
|
||||
* {
|
||||
* "message": "Not authorized",
|
||||
* "error_type": "authorization.unauthorized"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_UNAUTHORIZED = 'authorization.unauthorized';
|
||||
|
||||
/**
|
||||
* @apiDefine CaptchaError
|
||||
* @apiError (Error 429) {Object} info Additional info from server
|
||||
* @apiError (Error 429) {String} info.site_key Public site key for rendering reCaptcha
|
||||
*
|
||||
* @apiErrorExample {json} Captcha
|
||||
* HTTP/1.1 429 Too Many Requests
|
||||
* {
|
||||
* "message": "Invalid captcha",
|
||||
* "error_type": "authorization.captcha"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_CAPTCHA = 'authorization.captcha';
|
||||
|
||||
/**
|
||||
* @apiDefine LimiterError
|
||||
* @apiErrorExample {json} Limiter
|
||||
* HTTP/1.1 423 Locked
|
||||
* {
|
||||
* "message": "Enhance Your Calm",
|
||||
* "error_type": "authorization.banned_enhance_your_calm"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_BANNED = 'authorization.banned';
|
||||
|
||||
/**
|
||||
* @apiDefine TokenMismatchError
|
||||
* @apiErrorExample {json} Token mismatch
|
||||
* HTTP/1.1 401 Unauthorized
|
||||
* {
|
||||
* "message": "Token mismatch",
|
||||
* "error_type": "authorization.token_mismatch"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_TOKEN_MISMATCH = 'authorization.token_mismatch';
|
||||
|
||||
/**
|
||||
* @apiDefine TokenExpiredError
|
||||
* @apiErrorExample {json} Token expired
|
||||
* HTTP/1.1 401 Unauthorized
|
||||
* {
|
||||
* "message": "Token expired",
|
||||
* "error_type": "authorization.token_expired"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_TOKEN_EXPIRED = 'authorization.token_expired';
|
||||
|
||||
/**
|
||||
* @apiDefine UserDeactivatedError
|
||||
* @apiErrorExample {json} User deactivated
|
||||
* HTTP/1.1 403 Forbidden
|
||||
* {
|
||||
* "message": "User deactivated",
|
||||
* "error_type": "authorization.user_disabled"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_USER_DISABLED = 'authorization.user_disabled';
|
||||
|
||||
/**
|
||||
* @apiDeprecated since 4.0.0
|
||||
* @apiDefine ParamsValidationError
|
||||
* @apiErrorExample {json} Params validation
|
||||
* HTTP/1.1 400 Bad Request
|
||||
* {
|
||||
* "message": "Invalid params",
|
||||
* "error_type": "authorization.wrong_params"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_VALIDATION_FAILED = 'authorization.wrong_params';
|
||||
|
||||
/**
|
||||
* @apiDefine NoSuchUserError
|
||||
* @apiErrorExample {json} No such user
|
||||
* HTTP/1.1 404 Not Found
|
||||
* {
|
||||
* "message": "User with such email isn’t found",
|
||||
* "error_type": "authorization.user_not_found"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_USER_NOT_FOUND = 'authorization.user_not_found';
|
||||
|
||||
/**
|
||||
* @apiDefine InvalidPasswordResetDataError
|
||||
* @apiErrorExample {json} Invalid password reset data
|
||||
* HTTP/1.1 401 Unauthorized
|
||||
* {
|
||||
* "message": "Invalid password reset data",
|
||||
* "error_type": "authorization.invalid_password_data"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_INVALID_PASSWORD_RESET_DATA = 'authorization.invalid_password_data';
|
||||
|
||||
/**
|
||||
* @apiDefine ForbiddenError
|
||||
* @apiErrorExample {json} Forbidden
|
||||
* HTTP/1.1 403 Forbidden
|
||||
* {
|
||||
* "message": "Access denied to this item",
|
||||
* "error_type": "authorization.forbidden"
|
||||
* }
|
||||
*
|
||||
* @apiVersion 1.0.0
|
||||
*/
|
||||
public const ERROR_TYPE_FORBIDDEN = 'authorization.forbidden';
|
||||
|
||||
protected const ERRORS = [
|
||||
self::ERROR_TYPE_UNAUTHORIZED => ['code' => 401, 'message' => 'Not authorized'],
|
||||
self::ERROR_TYPE_CAPTCHA => ['code' => 429, 'message' => 'Invalid captcha',],
|
||||
self::ERROR_TYPE_BANNED => ['code' => 423, 'message' => 'Enhance Your Calm'],
|
||||
self::ERROR_TYPE_TOKEN_MISMATCH => ['code' => 401, 'message' => 'Token mismatch'],
|
||||
self::ERROR_TYPE_TOKEN_EXPIRED => ['code' => 401, 'message' => 'Token expired'],
|
||||
self::ERROR_TYPE_USER_DISABLED => ['code' => 403, 'message' => 'User deactivated'],
|
||||
self::ERROR_TYPE_VALIDATION_FAILED => ['code' => 400, 'message' => 'Invalid params'],
|
||||
self::ERROR_TYPE_USER_NOT_FOUND => ['code' => 404, 'message' => 'User with such email isn’t found'],
|
||||
self::ERROR_TYPE_INVALID_PASSWORD_RESET_DATA => ['code' => 401, 'message' => 'Invalid password reset data'],
|
||||
self::ERROR_TYPE_FORBIDDEN => ['code' => 403, 'message' => 'This action is unauthorized']
|
||||
];
|
||||
|
||||
public function __construct($type = self::ERROR_TYPE_UNAUTHORIZED)
|
||||
{
|
||||
$this->errorCode = $type;
|
||||
$this->status = self::ERRORS[$type]['code'];
|
||||
|
||||
parent::__construct(self::ERRORS[$type]['message']);
|
||||
}
|
||||
}
|
||||
23
app/Exceptions/Entities/DeprecatedApiException.php
Normal file
23
app/Exceptions/Entities/DeprecatedApiException.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class DeprecatedApiException extends HttpException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$lastCalledMethod = $this->getTrace()[0];
|
||||
$deprecatedMethod = "{$lastCalledMethod['class']}@{$lastCalledMethod['function']}";
|
||||
|
||||
\Log::warning("Deprecated method {$deprecatedMethod} called, update Cattr client", [
|
||||
'user_id' => auth()->user()->id ?? null
|
||||
]);
|
||||
|
||||
$this->errorCode = 'deprecation.api';
|
||||
$this->status = 400;
|
||||
|
||||
parent::__construct("Deprecated method {$deprecatedMethod} called, update Cattr client");
|
||||
}
|
||||
}
|
||||
14
app/Exceptions/Entities/InstallationException.php
Normal file
14
app/Exceptions/Entities/InstallationException.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class InstallationException extends HttpException
|
||||
{
|
||||
protected $status = 400;
|
||||
|
||||
protected $errorCode = 'app.installation';
|
||||
|
||||
protected $message = 'You need to run installation';
|
||||
}
|
||||
11
app/Exceptions/Entities/IntervalAlreadyDeletedException.php
Normal file
11
app/Exceptions/Entities/IntervalAlreadyDeletedException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class IntervalAlreadyDeletedException extends HttpException
|
||||
{
|
||||
protected $errorCode = 'interval_already_deleted';
|
||||
protected $status = 409;
|
||||
}
|
||||
12
app/Exceptions/Entities/InvalidMainException.php
Normal file
12
app/Exceptions/Entities/InvalidMainException.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class InvalidMainException extends HttpException
|
||||
{
|
||||
protected $status = 422;
|
||||
|
||||
protected $message = 'Base mistranslation detected';
|
||||
}
|
||||
12
app/Exceptions/Entities/MethodNotAllowedException.php
Normal file
12
app/Exceptions/Entities/MethodNotAllowedException.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class MethodNotAllowedException extends HttpException
|
||||
{
|
||||
protected $status = 405;
|
||||
|
||||
protected $errorCode = 'http.request.wrong_method';
|
||||
}
|
||||
15
app/Exceptions/Entities/NotEnoughRightsException.php
Normal file
15
app/Exceptions/Entities/NotEnoughRightsException.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class NotEnoughRightsException extends HttpException
|
||||
{
|
||||
public function __construct(string $message = 'Not enoughs rights')
|
||||
{
|
||||
$this->status = 403;
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
40
app/Exceptions/Entities/TaskRelationException.php
Normal file
40
app/Exceptions/Entities/TaskRelationException.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Entities;
|
||||
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
|
||||
class TaskRelationException extends HttpException
|
||||
{
|
||||
public const NOT_SAME_PROJECT = 'task_relation.not_same_project';
|
||||
public const CYCLIC = 'task_relation.cyclic';
|
||||
public const ALREADY_EXISTS = 'task_relation.already_exists';
|
||||
public const CANNOT_START_BEFORE_PARENT_ENDS = 'task_relation.cannot_start_before_parent_ends';
|
||||
|
||||
|
||||
public function __construct($type)
|
||||
{
|
||||
$ERRORS = [
|
||||
self::NOT_SAME_PROJECT => [
|
||||
'code' => 409,
|
||||
'message' => __("validation.tasks-relations.must_have_same_project")
|
||||
],
|
||||
self::CYCLIC => [
|
||||
'code' => 409,
|
||||
'message' => __("validation.tasks-relations.cyclic_relation_detected")
|
||||
],
|
||||
self::ALREADY_EXISTS => [
|
||||
'code' => 409,
|
||||
'message' => __("validation.tasks-relations.already_exists")
|
||||
],
|
||||
self::CANNOT_START_BEFORE_PARENT_ENDS => [
|
||||
'code' => 409,
|
||||
'message' => __("validation.tasks-relations.cannot_start_before_parent_ends")
|
||||
]
|
||||
];
|
||||
$this->errorCode = $type;
|
||||
$this->status = $ERRORS[$type]['code'];
|
||||
|
||||
parent::__construct($ERRORS[$type]['message']);
|
||||
}
|
||||
}
|
||||
132
app/Exceptions/Handler.php
Normal file
132
app/Exceptions/Handler.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use App\Exceptions\Entities\MethodNotAllowedException;
|
||||
use Crypt;
|
||||
use Filter;
|
||||
use Flugg\Responder\Exceptions\ConvertsExceptions;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Session\TokenMismatchException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use PDOException;
|
||||
use Str;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Flugg\Responder\Exceptions\Http\HttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class Handler
|
||||
*/
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
use ConvertsExceptions;
|
||||
|
||||
/**
|
||||
* A list of exception types with their corresponding custom log levels.
|
||||
*
|
||||
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
|
||||
*/
|
||||
protected $levels = [
|
||||
//
|
||||
];
|
||||
|
||||
/**
|
||||
* A list of the exception types that are not reported.
|
||||
*
|
||||
* @var array<int, class-string<\Throwable>>
|
||||
*/
|
||||
protected $dontReport
|
||||
= [
|
||||
AuthenticationException::class,
|
||||
AuthorizationException::class,
|
||||
Entities\AuthorizationException::class,
|
||||
HttpException::class,
|
||||
ModelNotFoundException::class,
|
||||
TokenMismatchException::class,
|
||||
ValidationException::class,
|
||||
PDOException::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* A list of the inputs that are never flashed to the session on validation exceptions.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $dontFlash = [
|
||||
'current_password',
|
||||
'password',
|
||||
'password_confirmation',
|
||||
];
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
$this->reportable(function (Throwable $e) {
|
||||
if (app()->bound('sentry')) {
|
||||
app('sentry')->captureException($e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default context variables for logging.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function context(): array
|
||||
{
|
||||
$traceId = Str::uuid()->toString();
|
||||
try {
|
||||
// Only add trace_id to error response if Filter::getErrorResponseFilterName() method exists
|
||||
Filter::listen(Filter::getErrorResponseFilterName(), static function (array|null $data = []) use ($traceId) {
|
||||
$data['trace_id'] = $traceId;
|
||||
return $data;
|
||||
});
|
||||
} catch (Throwable $exception) {
|
||||
}
|
||||
|
||||
$requestContent = collect(rescue(fn() => request()->all(), [], false))
|
||||
->map(function ($item, string $key) {
|
||||
if (Str::contains($key, ['screenshot', 'password', 'secret', 'token', 'api_key'], true)) {
|
||||
return '***';
|
||||
}
|
||||
return $item;
|
||||
})->toArray();
|
||||
|
||||
if (config('app.debug') === false){
|
||||
try {
|
||||
$requestContent = Crypt::encryptString(json_encode($requestContent, JSON_THROW_ON_ERROR));
|
||||
} catch (Throwable $exception) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return array_merge(parent::context(), [
|
||||
'trace_id' => $traceId,
|
||||
'request_uri' => request()->getRequestUri(),
|
||||
'request_content' => $requestContent
|
||||
]);
|
||||
}
|
||||
|
||||
public function render($request, $e): Response
|
||||
{
|
||||
$this->convert($e, [
|
||||
MethodNotAllowedHttpException::class => fn($e) => throw new MethodNotAllowedException($e->getMessage()),
|
||||
AuthenticationException::class => fn($e
|
||||
) => throw new Entities\AuthorizationException(Entities\AuthorizationException::ERROR_TYPE_UNAUTHORIZED),
|
||||
]);
|
||||
|
||||
$this->convertDefaultException($e);
|
||||
|
||||
if ($e instanceof HttpException) {
|
||||
return $this->renderResponse($e);
|
||||
}
|
||||
|
||||
return responder()->error($e->getCode(), $e->getMessage())->respond();
|
||||
}
|
||||
}
|
||||
16
app/Exceptions/Interfaces/InfoExtendedException.php
Normal file
16
app/Exceptions/Interfaces/InfoExtendedException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Interfaces;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Interface ReasonableException
|
||||
*/
|
||||
interface InfoExtendedException extends Throwable
|
||||
{
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getInfo();
|
||||
}
|
||||
16
app/Exceptions/Interfaces/TypedException.php
Normal file
16
app/Exceptions/Interfaces/TypedException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Interfaces;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Interface TypedException
|
||||
*/
|
||||
interface TypedException extends Throwable
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string;
|
||||
}
|
||||
Reference in New Issue
Block a user