Last active
June 20, 2021 06:27
-
-
Save osibg/c58afbe2616a584751caab98c3f0b33e to your computer and use it in GitHub Desktop.
Handler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Exceptions; | |
use App\Exceptions\Handlers; | |
use Throwable; | |
use GuzzleHttp\Exception\RequestException; | |
use Illuminate\Auth\AuthenticationException; | |
use Illuminate\Database\QueryException; | |
use Illuminate\Http\JsonResponse; | |
use Illuminate\Http\Request; | |
use Illuminate\Validation\ValidationException; | |
use Illuminate\Auth\Access\AuthorizationException; | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler; | |
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; | |
class Handler extends ExceptionHandler | |
{ | |
protected $dontReport = [ | |
AuthorizationException::class, | |
HttpException::class, | |
ModelNotFoundException::class, | |
ValidationException::class, | |
]; | |
protected $handlerCasts = [ | |
ModelNotFoundException::class => Handlers\ModelNotFoundException::class, | |
QueryException::class => Handlers\QueryException::class, | |
RequestException::class => Handlers\RequestException::class, | |
ValidationException::class => Handlers\ValidationException::class, | |
FileCannotBeAdded::class => Handlers\FileCannotBeAdded::class, | |
AuthorizationException::class => Handlers\AuthorizationException::class, | |
UnauthorizedHttpException::class => Handlers\AuthorizationException::class, | |
AuthenticationException::class => Handlers\AuthenticationException::class, | |
MethodNotAllowedHttpException::class => Handlers\MethodNotAllowedHttpException::class, | |
NotFoundHttpException::class => Handlers\NotFoundHttpException::class, | |
]; | |
public function render($request, Throwable $exception) | |
{ | |
if (($request instanceof Request && !$request->expectsJson()) || config('app.debug', false)) { | |
return parent::render($request, $exception); | |
} | |
foreach ($this->handlerCasts as $class => $handlerClass) { | |
if (!$exception instanceof $class) { | |
continue; | |
} | |
$response = (new $handlerClass)($exception); | |
if (isset($response)) { | |
return $response; | |
} | |
} | |
return response()->json(['error' => $exception->getMessage()], 500); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment