Compare commits

...

3 Commits

1 changed files with 14 additions and 1 deletions

View File

@ -4,9 +4,11 @@ namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Http\{Request};
use Illuminate\Http\{Request, Response};
use Illuminate\Validation\ValidationException;
use Slowlyo\OwlAdmin\Exceptions\AdminException;
use Illuminate\Auth\AuthenticationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler
{
@ -34,6 +36,10 @@ class Handler extends ExceptionHandler
$this->renderable(function (RuntimeException $e, Request $request) {
return response(['code' => $e->getCode(), 'message' => $e->getMessage()], $e->getHttpStatusCode());
});
$this->renderable(function (NotFoundHttpException $e, Request $request) {
return response(['code' => Response::HTTP_NOT_FOUND, 'message' => $e->getMessage()], Response::HTTP_NOT_FOUND);
});
}
protected function invalidJson($request, ValidationException $exception)
@ -44,4 +50,11 @@ class Handler extends ExceptionHandler
'errors' => $exception->errors(),
], $exception->status);
}
protected function unauthenticated($request, AuthenticationException $exception)
{
return $this->shouldReturnJson($request, $exception)
? response()->json(['code' => Response::HTTP_UNAUTHORIZED, 'message' => $exception->getMessage()], 401)
: redirect()->guest($exception->redirectTo() ?? route('login'));
}
}