To begin with I wanted to catch the abort 403 error wich I tought would trow an AccessDeniedHttpException so I tried to catch the error like this.
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Event;
use App\Events\UserNotAllowed;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
HttpException::class,
ModelNotFoundException::class,
AccessDeniedHttpException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof AccessDeniedHttpException) {
Event::fire(new UserNotAllowed($request));
return redirect()->to("/home");
}
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
Unforcenately this does not work becouse only 404 errors are given the right class whilst all others are given a general exeption wich you should be able to catch the number from like this.
public function render($request, Exception $e)
{
// Or ===
if ($e->getStatusCode() == 403) {
Event::fire(new UserNotAllowed($request));
return redirect()->to("/home");
}
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
But when doing this all other error pages fail to work giving a general 500 error in my webrowser Is there an easy way to listen or simpaly catch the errors without changing the whole src abort function from laravel. Thank you in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire