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
|
|
|
parent::report($exception); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Render an exception into an HTTP response. |
46
|
|
|
* |
47
|
|
|
* @param \Illuminate\Http\Request $request |
48
|
|
|
* @param \Exception $exception |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function render($request, Exception $exception) |
53
|
|
|
{ |
54
|
|
|
if ($exception instanceof AuthorizationException) { |
55
|
|
|
flash('You do not have permissions to perform this action.')->error(); |
56
|
|
|
|
57
|
|
|
return redirect()->route('home'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($exception instanceof TokenMismatchException) { |
61
|
|
|
flash('Your session has expired and is now restored. Please, try again.')->error(); |
62
|
|
|
|
63
|
|
|
return redirect()->back(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($exception instanceof MethodNotAllowedHttpException) { |
67
|
|
|
throw new NotFoundHttpException($exception->getMessage(), $exception); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return parent::render($request, $exception); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Convert an authentication exception into an unauthenticated response. |
75
|
|
|
* |
76
|
|
|
* @param \Illuminate\Http\Request $request |
77
|
|
|
* @param AuthenticationException $exception |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse |
80
|
|
|
*/ |
81
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if ($request->expectsJson()) { |
84
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return redirect()->guest(route('login')); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.