1 | <?php |
||
2 | |||
3 | namespace App\Exceptions; |
||
4 | |||
5 | use Exception; |
||
6 | use Illuminate\Auth\AuthenticationException; |
||
7 | use Illuminate\Session\TokenMismatchException; |
||
8 | use Illuminate\Validation\ValidationException; |
||
9 | use Illuminate\Auth\Access\AuthorizationException; |
||
10 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
||
11 | use Symfony\Component\HttpKernel\Exception\HttpException; |
||
12 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
||
13 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
14 | use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
||
15 | |||
16 | class Handler extends ExceptionHandler |
||
17 | { |
||
18 | /** |
||
19 | * A list of the exception types that should not be reported. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $dontReport = [ |
||
24 | AuthenticationException::class, |
||
25 | AuthorizationException::class, |
||
26 | HttpException::class, |
||
27 | ModelNotFoundException::class, |
||
28 | TokenMismatchException::class, |
||
29 | ValidationException::class, |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * Report or log an exception. |
||
34 | * |
||
35 | * This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
||
36 | * |
||
37 | * @param \Exception $exception |
||
38 | */ |
||
39 | public function report(Exception $exception) |
||
40 | { |
||
41 | if ($this->shouldReport($exception)) { |
||
42 | app('sentry')->captureException($exception); |
||
43 | } |
||
44 | |||
45 | parent::report($exception); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Render an exception into an HTTP response. |
||
50 | * |
||
51 | * @param \Illuminate\Http\Request $request |
||
52 | * @param \Exception $exception |
||
53 | * |
||
54 | * @return mixed |
||
55 | */ |
||
56 | public function render($request, Exception $exception) |
||
57 | { |
||
58 | if ($exception instanceof AuthorizationException) { |
||
59 | flash('You do not have permissions to perform this action.')->error(); |
||
60 | |||
61 | return redirect()->route('dashboard'); |
||
62 | } |
||
63 | |||
64 | if ($exception instanceof TokenMismatchException) { |
||
65 | flash('Your session has expired and is now restored. Please, try again.')->error(); |
||
66 | |||
67 | return redirect()->back(); |
||
68 | } |
||
69 | |||
70 | if ($exception instanceof MethodNotAllowedHttpException) { |
||
71 | throw new NotFoundHttpException($exception->getMessage(), $exception); |
||
72 | } |
||
73 | |||
74 | return parent::render($request, $exception); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Convert an authentication exception into an unauthenticated response. |
||
79 | * |
||
80 | * @param \Illuminate\Http\Request $request |
||
81 | * @param AuthenticationException $exception |
||
82 | * |
||
83 | * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse |
||
84 | */ |
||
85 | protected function unauthenticated($request, AuthenticationException $exception) |
||
86 | { |
||
87 | if ($request->expectsJson()) { |
||
88 | return response()->json(['error' => 'Unauthenticated.'], 401); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
89 | } |
||
90 | |||
91 | return redirect()->guest(route('login')); |
||
92 | } |
||
93 | } |
||
94 |