1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of laravel.su package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Exceptions; |
11
|
|
|
|
12
|
|
|
use Illuminate\Http\Response; |
13
|
|
|
use Illuminate\Http\RedirectResponse; |
14
|
|
|
use Illuminate\Auth\AuthenticationException; |
15
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
17
|
|
|
use Whoops\Handler\PrettyPageHandler; |
18
|
|
|
use Whoops\Run; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Handler. |
22
|
|
|
* Класс обработки всех исключений в нашем приложении. |
23
|
|
|
* Тут мы их будем обрабатывать и отображать ошибки, в случае проблем. |
24
|
|
|
*/ |
25
|
|
|
class Handler extends ExceptionHandler |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Список исключений, которые являются частью нормальной работы приложения |
29
|
|
|
* и которые не надо как-то обрабатывать. Например, "ошибка 404" и прочие. |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $dontReport = [ |
33
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
34
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
35
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
36
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
37
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
38
|
|
|
\Illuminate\Validation\ValidationException::class, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Метод, куда прилетают все наши исключения для обработки. |
43
|
|
|
* Отличное место для отправки оных в Sentry, Bugsnag, и проч. |
44
|
|
|
* @param \Exception $exception |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
|
|
public function report(\Exception $exception): void |
48
|
|
|
{ |
49
|
|
|
if ($this->shouldReport($exception) && app('app')->bound('sentry')) { |
50
|
|
|
app('sentry')->captureException($exception); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
parent::report($exception); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Отображение наших необработанных ошибок. |
58
|
|
|
* @param \Illuminate\Http\Request $request |
59
|
|
|
* @param \Exception $exception |
60
|
|
|
* @return string|\Symfony\Component\HttpFoundation\Response |
61
|
|
|
* @throws \Throwable |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
public function render($request, \Exception $exception) |
65
|
|
|
{ |
66
|
|
|
$exception = $this->prepareException($exception); |
67
|
|
|
|
68
|
|
|
$htmlAccepted = ! $request->ajax() && $request->acceptsHtml(); |
69
|
|
|
|
70
|
|
|
if ($htmlAccepted && ! config('app.debug')) { |
71
|
|
|
$whoops = new Run(); |
72
|
|
|
$whoops->pushHandler(new PrettyPageHandler()); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $whoops->handleException($exception); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!$this->isHttpException($exception)) { |
78
|
|
|
$exception = new HttpException(500, 'Be right back.', $exception); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return response($this->getErrorView($exception)->render(), $exception->getStatusCode()); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param HttpException $exception |
86
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
87
|
|
|
*/ |
88
|
|
|
private function getErrorView(HttpException $exception) |
89
|
|
|
{ |
90
|
|
|
return view('layout.error', [ |
91
|
|
|
'message' => Response::$statusTexts[$exception->getStatusCode()], |
92
|
|
|
'code' => $exception->getStatusCode(), |
93
|
|
|
'error' => $exception, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Преобразовываем ошибки аутентификации в разлогинивающий ответ. |
99
|
|
|
* @param \Illuminate\Http\Request $request |
100
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
101
|
|
|
* @return Response|RedirectResponse |
102
|
|
|
*/ |
103
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
104
|
|
|
{ |
105
|
|
|
if ($request->expectsJson()) { |
106
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return redirect()->guest('login') |
110
|
|
|
->withException($exception); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: