2023-09-20 11:42:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Exceptions;
|
|
|
|
|
|
2023-10-02 09:35:06 +02:00
|
|
|
use App\Enums\Api\ApiError;
|
|
|
|
|
use App\Enums\Api\ApiStatus;
|
|
|
|
|
use App\Http\Resources\Api\ApiResponse;
|
|
|
|
|
use App\Lib\ApiHelper;
|
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
|
use Illuminate\Http\Exceptions\ThrottleRequestsException;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
2023-09-20 11:42:42 +02:00
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2023-10-02 09:35:06 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
2023-09-20 11:42:42 +02:00
|
|
|
|
|
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* A list of the exception types that are not reported.
|
|
|
|
|
*
|
2023-10-02 09:35:06 +02:00
|
|
|
* @var array
|
2023-09-20 11:42:42 +02:00
|
|
|
*/
|
2023-10-02 09:35:06 +02:00
|
|
|
protected $dontReport = [
|
|
|
|
|
ApiErrorException::class,
|
|
|
|
|
RedirectTo::class
|
|
|
|
|
];
|
2023-09-20 11:42:42 +02:00
|
|
|
|
|
|
|
|
/**
|
2023-10-02 09:35:06 +02:00
|
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
2023-09-20 11:42:42 +02:00
|
|
|
*
|
2023-10-02 09:35:06 +02:00
|
|
|
* @var array
|
2023-09-20 11:42:42 +02:00
|
|
|
*/
|
|
|
|
|
protected $dontFlash = [
|
|
|
|
|
'password',
|
|
|
|
|
'password_confirmation',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register the exception handling callbacks for the application.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-10-02 09:35:06 +02:00
|
|
|
public function register(): void
|
2023-09-20 11:42:42 +02:00
|
|
|
{
|
2023-10-02 09:35:06 +02:00
|
|
|
$this->reportable(function (\Throwable $e) {
|
2023-09-20 11:42:42 +02:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-02 09:35:06 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $request
|
|
|
|
|
* @param \Throwable $e
|
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
|
*/
|
|
|
|
|
public function render(mixed $request, \Throwable $e): Response
|
|
|
|
|
{
|
|
|
|
|
if (ApiHelper::isRequestForApi($request) and $request->isJson()) {
|
|
|
|
|
return $this->handleApiException($request, $e);
|
|
|
|
|
} //end if
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
return parent::render($request, $e);
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a json response for any apiV4 error case.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param \Throwable $e
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
*/
|
|
|
|
|
private function handleApiException(Request $request, \Throwable $e): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
switch (get_class($e)) {
|
|
|
|
|
case AuthenticationException::class:
|
|
|
|
|
/** @var AuthenticationException $e */
|
|
|
|
|
return $this->getReponse(
|
|
|
|
|
$request,
|
|
|
|
|
401,
|
|
|
|
|
ApiError::apiAuthFailed->value(),
|
|
|
|
|
sprintf('%s, %s', ApiError::apiAuthFailed->label(), $e->getMessage())
|
|
|
|
|
);
|
|
|
|
|
case MethodNotAllowedHttpException::class:
|
|
|
|
|
/** @var MethodNotAllowedHttpException $e */
|
|
|
|
|
return $this->getReponse(
|
|
|
|
|
$request,
|
|
|
|
|
$e->getStatusCode(),
|
|
|
|
|
ApiError::methodNotAllowed->value(),
|
|
|
|
|
sprintf('%s, %s', ApiError::methodNotAllowed->label(), $e->getMessage())
|
|
|
|
|
);
|
|
|
|
|
case ValidationException::class:
|
|
|
|
|
/** @var ValidationException $e */
|
|
|
|
|
return $this->getReponse(
|
|
|
|
|
$request,
|
|
|
|
|
$e->status,
|
|
|
|
|
ApiError::validationFailed->value(),
|
|
|
|
|
sprintf(ApiError::validationFailed->label(), $e->getMessage()),
|
|
|
|
|
['errors' => $e->errors()]
|
|
|
|
|
);
|
|
|
|
|
case ModelNotFoundException::class:
|
|
|
|
|
/** @var ModelNotFoundException $e */
|
|
|
|
|
return $this->getReponse(
|
|
|
|
|
$request,
|
|
|
|
|
404,
|
|
|
|
|
ApiError::modelNotFound->value(),
|
|
|
|
|
sprintf(
|
|
|
|
|
ApiError::modelNotFound->label(),
|
|
|
|
|
\array_slice(explode('\\', $e->getModel()), -1)[0]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
case ApiErrorException::class:
|
|
|
|
|
/** @var ApiErrorException $e */
|
|
|
|
|
return $this->getReponse(
|
|
|
|
|
$request,
|
|
|
|
|
$e->getStatusCode(),
|
|
|
|
|
$e->getCode(),
|
|
|
|
|
$e->getMessage()
|
|
|
|
|
);
|
|
|
|
|
case ThrottleRequestsException::class:
|
|
|
|
|
/** @var ThrottleRequestsException $e */
|
|
|
|
|
return $this->getReponse(
|
|
|
|
|
$request,
|
|
|
|
|
429,
|
|
|
|
|
ApiError::throttleRequestsException->value(),
|
|
|
|
|
ApiError::throttleRequestsException->label()
|
|
|
|
|
);
|
|
|
|
|
case AuthorizationException::class:
|
|
|
|
|
/** @var AuthorizationException $e */
|
|
|
|
|
return $this->getReponse(
|
|
|
|
|
$request,
|
|
|
|
|
403,
|
|
|
|
|
ApiError::authorizationException->value(),
|
|
|
|
|
ApiError::authorizationException->label()
|
|
|
|
|
);
|
|
|
|
|
} //end switch
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
if (config('app.env') === 'testing') {
|
|
|
|
|
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Found
|
|
|
|
|
print_r((string)$e);
|
|
|
|
|
}
|
|
|
|
|
return $this->getReponse($request, 500, \intval($e->getCode()), $e->getMessage());
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate an Api json response.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param integer $httpCode
|
|
|
|
|
* @param integer $apiCode
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
*/
|
|
|
|
|
private static function getReponse(
|
|
|
|
|
Request $request,
|
|
|
|
|
int $httpCode,
|
|
|
|
|
int $apiCode,
|
|
|
|
|
string $message,
|
|
|
|
|
array $data = []
|
|
|
|
|
): JsonResponse {
|
|
|
|
|
return (new ApiResponse(
|
|
|
|
|
(object)array_merge($data, ['message' => $message, 'code' => $apiCode]),
|
|
|
|
|
ApiStatus::KO
|
|
|
|
|
))->toResponse($request)->setStatusCode($httpCode);
|
|
|
|
|
}
|
2023-09-20 11:42:42 +02:00
|
|
|
}
|