1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
7
|
|
|
use Illuminate\Auth\AuthenticationException; |
8
|
|
|
use Illuminate\Config\Repository as Config; |
9
|
|
|
use Illuminate\Contracts\Container\Container; |
10
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
11
|
|
|
use Illuminate\Foundation\Application; |
12
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
13
|
|
|
use Illuminate\Routing\Redirector; |
14
|
|
|
use Illuminate\Routing\ResponseFactory; |
15
|
|
|
use Illuminate\Routing\UrlGenerator; |
16
|
|
|
use Illuminate\Support\Str; |
17
|
|
|
use Illuminate\Validation\ValidationException; |
18
|
|
|
use Illuminate\View\Factory as ViewFactory; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
20
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
21
|
|
|
|
22
|
|
|
class Handler extends ExceptionHandler |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Exceptions excluded from reporting. |
26
|
|
|
* |
27
|
|
|
* @var string[] |
28
|
|
|
*/ |
29
|
|
|
protected $dontReport = [ |
30
|
|
|
AuthorizationException::class, |
31
|
|
|
HttpException::class, |
32
|
|
|
ModelNotFoundException::class, |
33
|
|
|
ValidationException::class, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $isJsonOutputFormat = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Redirector |
43
|
|
|
*/ |
44
|
|
|
protected $redirector; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var UrlGenerator |
48
|
|
|
*/ |
49
|
|
|
protected $urlGenerator; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Application |
53
|
|
|
*/ |
54
|
|
|
protected $app; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var ViewFactory |
58
|
|
|
*/ |
59
|
|
|
protected $viewFactory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var ResponseFactory |
63
|
|
|
*/ |
64
|
|
|
protected $responseFactory; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Config |
68
|
|
|
*/ |
69
|
|
|
protected $config; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Container $container |
73
|
|
|
* @param Redirector $redirector |
74
|
|
|
* @param UrlGenerator $urlGenerator |
75
|
|
|
* @param Application $app |
76
|
|
|
* @param ViewFactory $viewFactory |
77
|
|
|
* @param ResponseFactory $responseFactory |
78
|
|
|
* @param Config $config |
79
|
|
|
*/ |
80
|
50 |
|
public function __construct( |
81
|
|
|
Container $container, |
82
|
|
|
Redirector $redirector, |
83
|
|
|
UrlGenerator $urlGenerator, |
84
|
|
|
Application $app, |
85
|
|
|
ViewFactory $viewFactory, |
86
|
|
|
ResponseFactory $responseFactory, |
87
|
|
|
Config $config |
88
|
|
|
) { |
89
|
50 |
|
parent::__construct($container); |
90
|
|
|
|
91
|
50 |
|
$this->redirector = $redirector; |
92
|
50 |
|
$this->urlGenerator = $urlGenerator; |
93
|
50 |
|
$this->app = $app; |
94
|
50 |
|
$this->viewFactory = $viewFactory; |
95
|
50 |
|
$this->responseFactory = $responseFactory; |
96
|
50 |
|
$this->config = $config; |
97
|
50 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Exception $e |
101
|
|
|
* @return mixed |
102
|
|
|
* @throws Exception |
103
|
|
|
*/ |
104
|
8 |
|
public function report(Exception $e) |
105
|
|
|
{ |
106
|
8 |
|
return parent::report($e); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \Illuminate\Http\Request $request |
111
|
|
|
* @param Exception $e |
112
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|string|Response |
|
|
|
|
113
|
|
|
*/ |
114
|
8 |
|
public function render($request, Exception $e) |
115
|
|
|
{ |
116
|
8 |
|
$e = $this->prepareException($e); |
117
|
|
|
|
118
|
8 |
|
$this->isJsonOutputFormat = $request->ajax() || $request->wantsJson(); |
119
|
|
|
|
120
|
8 |
|
if ($e instanceof HttpResponseException) { |
121
|
|
|
return $e->getResponse(); |
122
|
8 |
|
} elseif ($e instanceof AuthenticationException) { |
123
|
1 |
|
return $this->handleUnauthenticated($request, $e); |
124
|
7 |
|
} elseif ($e instanceof ValidationException) { |
125
|
|
|
return $this->convertValidationExceptionToResponse($e, $request); |
126
|
|
|
} |
127
|
|
|
|
128
|
7 |
|
if ($this->isJsonOutputFormat) { |
129
|
|
|
return $this->prepareJsonResponse($request, $e); |
130
|
|
|
} |
131
|
|
|
|
132
|
7 |
|
return $this->renderHttpExceptionWithView($request, $e); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param \Illuminate\Http\Request $request |
137
|
|
|
* @param \Exception $e |
138
|
|
|
* @return \Illuminate\Http\Response|Response |
139
|
|
|
*/ |
140
|
7 |
|
public function renderHttpExceptionWithView($request, $e) |
141
|
|
|
{ |
142
|
7 |
|
if ($this->config->get('app.debug')) { |
143
|
|
|
return $this->convertExceptionToResponse($e); |
144
|
|
|
} |
145
|
|
|
|
146
|
7 |
|
$statusCode = $this->isHttpException($e) ? $e->getStatusCode() : 500; |
|
|
|
|
147
|
7 |
|
$headers = $this->isHttpException($e) ? $e->getHeaders() : []; |
|
|
|
|
148
|
|
|
|
149
|
7 |
|
$isSubdomainAdmin = empty($this->config->get('twill.admin_app_path')) && $request->getHost() == $this->config->get('twill.admin_app_url'); |
150
|
7 |
|
$isSubdirectoryAdmin = !empty($this->config->get('twill.admin_app_path')) && Str::startsWith($request->path(), $this->config->get('twill.admin_app_path')); |
151
|
|
|
|
152
|
7 |
|
if ($isSubdomainAdmin || $isSubdirectoryAdmin) { |
153
|
5 |
|
$view = $this->viewFactory->exists("admin.errors.$statusCode") ? "admin.errors.$statusCode" : "twill::errors.$statusCode"; |
154
|
|
|
} else { |
155
|
2 |
|
$view = $this->config->get('twill.frontend.views_path') . ".errors.{$statusCode}"; |
156
|
|
|
} |
157
|
|
|
|
158
|
7 |
|
if ($this->viewFactory->exists($view)) { |
159
|
5 |
|
return $this->responseFactory->view($view, ['exception' => $e], $statusCode, $headers); |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
if ($this->isHttpException($e)) { |
163
|
2 |
|
return $this->renderHttpException($e); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return parent::render($request, $e); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param \Illuminate\Http\Request $request |
171
|
|
|
* @param AuthenticationException $exception |
172
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
173
|
|
|
*/ |
174
|
1 |
|
protected function handleUnauthenticated($request, AuthenticationException $exception) |
|
|
|
|
175
|
|
|
{ |
176
|
1 |
|
if ($request->expectsJson()) { |
177
|
|
|
return $this->responseFactory->json(['error' => 'Unauthenticated.'], 401); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
return $this->redirector->guest($this->urlGenerator->route('admin.login')); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param \Illuminate\Http\Request $request |
185
|
|
|
* @param ValidationException $exception |
186
|
|
|
* @return \Illuminate\Http\JsonResponse |
187
|
|
|
*/ |
188
|
|
|
protected function invalidJson($request, ValidationException $exception) |
189
|
|
|
{ |
190
|
|
|
return $this->responseFactory->json($exception->errors(), $exception->status); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.