|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Fort\Handlers; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Illuminate\Auth\AuthenticationException; |
|
9
|
|
|
use Rinvex\Fort\Exceptions\GenericException; |
|
10
|
|
|
use Illuminate\Foundation\Exceptions\Handler; |
|
11
|
|
|
use Rinvex\Fort\Exceptions\AuthorizationException; |
|
12
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
13
|
|
|
|
|
14
|
|
|
class ExceptionHandler extends Handler |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Render an exception into an HTTP response. |
|
18
|
|
|
* |
|
19
|
|
|
* @param \Illuminate\Http\Request $request |
|
20
|
|
|
* @param \Exception $exception |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
23
|
|
|
*/ |
|
24
|
|
|
public function render($request, Exception $exception) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($exception instanceof ModelNotFoundException) { |
|
27
|
|
|
$model = str_replace('Contract', '', $exception->getModel()); |
|
28
|
|
|
$isAdminarea = mb_strpos($request->route()->getName(), 'adminarea') !== false; |
|
29
|
|
|
$single = mb_strtolower(mb_substr($model, mb_strrpos($model, '\\') + 1)); |
|
30
|
|
|
$plural = str_plural($single); |
|
31
|
|
|
|
|
32
|
|
|
return intend([ |
|
33
|
|
|
'url' => $isAdminarea ? route("adminarea.{$plural}.index") : route('frontarea.home'), |
|
34
|
|
|
'with' => ['warning' => trans('messages.resource_not_found', ['resource' => $single, 'id' => $request->route()->parameter($single)])], |
|
|
|
|
|
|
35
|
|
|
], 404); |
|
36
|
|
|
} elseif ($exception instanceof AuthorizationException) { |
|
37
|
|
|
return intend([ |
|
38
|
|
|
'url' => '/', |
|
39
|
|
|
'with' => ['warning' => $exception->getMessage()], |
|
40
|
|
|
], 403); |
|
41
|
|
|
} elseif ($exception instanceof GenericException) { |
|
42
|
|
|
return intend([ |
|
43
|
|
|
'url' => $exception->getRedirection() ?? route('frontarea.home'), |
|
44
|
|
|
'withInput' => $exception->getInputs() ?? $request->all(), |
|
45
|
|
|
'with' => ['warning' => $exception->getMessage()], |
|
46
|
|
|
], 422); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return parent::render($request, $exception); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Convert an authentication exception into an unauthenticated response. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Illuminate\Http\Request $request |
|
56
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
61
|
|
|
{ |
|
62
|
|
|
return intend([ |
|
63
|
|
|
'url' => route('frontarea.login'), |
|
64
|
|
|
'withErrors' => ['rinvex.fort.session.required' => trans('messages.auth.session.required')], |
|
65
|
|
|
], 401); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.