|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Auth\AuthenticationException; |
|
7
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @codeCoverageIgnore |
|
14
|
|
|
*/ |
|
15
|
|
|
class Handler extends ExceptionHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* A list of the exception types that should not be reported. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $dontReport = [ |
|
23
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
|
24
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
|
25
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
|
26
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
|
27
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
|
28
|
|
|
\Illuminate\Validation\ValidationException::class, |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Report or log an exception. |
|
33
|
|
|
* |
|
34
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Exception $exception |
|
37
|
|
|
* @return void |
|
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
|
|
|
* @return \Illuminate\Http\Response |
|
50
|
|
|
*/ |
|
51
|
|
|
public function render($request, Exception $exception) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($exception instanceof NotFoundHttpException) { |
|
54
|
|
|
return response()->view('layouts.errors.404', [], 404); |
|
55
|
|
|
} elseif ($exception instanceof HttpException && $exception->getStatusCode() == 403) { |
|
56
|
|
|
return response()->view( |
|
57
|
|
|
'layouts.errors.403', |
|
58
|
|
|
['error' => 'Sorry, this page is restricted to authorized users only.'], |
|
59
|
|
|
403 |
|
60
|
|
|
); |
|
61
|
|
|
} elseif ($exception instanceof HttpException) { |
|
62
|
|
|
Log::info($exception->getMessage()); |
|
63
|
|
|
return response()->view('layouts.errors.503', ['error' => $exception->getTrace()], 500); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return parent::render($request, $exception); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Convert an authentication exception into an unauthenticated response. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Illuminate\Http\Request $request |
|
73
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
|
74
|
|
|
* @return \Illuminate\Http\Response |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($request->expectsJson()) { |
|
79
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return redirect()->guest(route('login')); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|