update project
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Api;
|
||||
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Throwable;
|
||||
|
||||
final class ApiExceptionRenderer
|
||||
{
|
||||
public function render(Throwable $e): JsonResponse
|
||||
{
|
||||
if ($e instanceof ValidationException) {
|
||||
return ApiResponse::error('Validation failed.', 422, $e->errors());
|
||||
}
|
||||
|
||||
if ($e instanceof AuthenticationException) {
|
||||
return ApiResponse::error('Unauthenticated.', 401);
|
||||
}
|
||||
|
||||
if ($e instanceof AuthorizationException) {
|
||||
return ApiResponse::error('Forbidden.', 403);
|
||||
}
|
||||
|
||||
if ($e instanceof ModelNotFoundException || $e instanceof NotFoundHttpException || str_contains($e::class, 'NotFound')) {
|
||||
return ApiResponse::error('Resource not found.', 404);
|
||||
}
|
||||
|
||||
if (str_contains($e::class, 'Conflict') || str_contains($e::class, 'Duplicate')) {
|
||||
return ApiResponse::error('Conflict.', 409);
|
||||
}
|
||||
|
||||
if (str_contains($e::class, 'Unauthorized')) {
|
||||
return ApiResponse::error('Forbidden.', 403);
|
||||
}
|
||||
|
||||
if (str_contains($e::class, 'Exception')) {
|
||||
report($e);
|
||||
}
|
||||
|
||||
return ApiResponse::error('Server error.', 500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user