1 | <?php |
||||||
2 | |||||||
3 | namespace Thinktomorrow\Chief\App\Exceptions; |
||||||
4 | |||||||
5 | use Illuminate\Auth\Access\AuthorizationException; |
||||||
6 | use Illuminate\Auth\AuthenticationException; |
||||||
7 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
||||||
8 | use Illuminate\Http\Exceptions\PostTooLargeException; |
||||||
9 | use Illuminate\Support\Arr; |
||||||
10 | use Illuminate\Support\Str; |
||||||
11 | use Illuminate\Validation\ValidationException; |
||||||
12 | use Throwable; |
||||||
13 | |||||||
14 | class ChiefExceptionHandler extends ExceptionHandler |
||||||
15 | { |
||||||
16 | /** |
||||||
17 | * A list of the exception types that should not be reported. |
||||||
18 | * |
||||||
19 | * @var array |
||||||
20 | */ |
||||||
21 | protected $dontReport = [ |
||||||
22 | // |
||||||
23 | ]; |
||||||
24 | |||||||
25 | /** |
||||||
26 | * A list of the inputs that are never flashed for validation exceptions. |
||||||
27 | * |
||||||
28 | * @var array |
||||||
29 | */ |
||||||
30 | protected $dontFlash = [ |
||||||
31 | 'password', |
||||||
32 | 'password_confirmation', |
||||||
33 | ]; |
||||||
34 | |||||||
35 | /** |
||||||
36 | * Render an exception into an HTTP response. |
||||||
37 | * |
||||||
38 | * @param \Illuminate\Http\Request $request |
||||||
39 | * @param Throwable $e |
||||||
40 | * @return \Illuminate\Http\Response |
||||||
41 | */ |
||||||
42 | public function render($request, Throwable $e) |
||||||
43 | { |
||||||
44 | if ($e instanceof AuthorizationException) { |
||||||
45 | return $this->unauthorized($request, $e); |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||
46 | } |
||||||
47 | |||||||
48 | // if ($request->getMethod() == 'POST' && $e instanceof PostTooLargeException) { |
||||||
49 | // if ($request->expectsJson()) { |
||||||
50 | // return response()->json([ |
||||||
51 | // 'error' => true, // required by redactor |
||||||
52 | // 'message' => $e->getMessage(), |
||||||
53 | // ], 200); |
||||||
54 | // } |
||||||
55 | // } |
||||||
56 | if ($this->shouldRenderChiefException($e)) { |
||||||
57 | return $this->renderChiefException($request, $e); |
||||||
0 ignored issues
–
show
|
|||||||
58 | } |
||||||
59 | |||||||
60 | return parent::render($request, $e); |
||||||
61 | } |
||||||
62 | |||||||
63 | private function shouldRenderChiefException(Throwable $exception): bool |
||||||
64 | { |
||||||
65 | return (Str::startsWith(request()->path(), 'admin/') && ! $exception instanceof AuthenticationException && ! $exception instanceof ValidationException); |
||||||
66 | } |
||||||
67 | |||||||
68 | protected function renderChiefException(\Illuminate\Http\Request $request, Throwable $exception) |
||||||
69 | { |
||||||
70 | if (! config('app.debug')) { |
||||||
71 | if ($request->expectsJson()) { |
||||||
72 | return response()->json(['error' => 'Something went wrong.'], 404); |
||||||
73 | } |
||||||
74 | |||||||
75 | return response()->view('chief::errors.custom', [], 500); |
||||||
76 | } |
||||||
77 | |||||||
78 | return parent::render($request, $exception); |
||||||
79 | } |
||||||
80 | |||||||
81 | |||||||
82 | protected function unauthorized(\Illuminate\Http\Request $request, AuthorizationException $exception) |
||||||
0 ignored issues
–
show
The parameter
$exception is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
83 | { |
||||||
84 | return redirect()->route('chief.back.dashboard') |
||||||
85 | ->with('messages.error', 'Oeps. Het lijkt erop dat je geen toegang hebt tot dit deel van chief. Vraag even de beheerder voor meer info.'); |
||||||
86 | } |
||||||
87 | |||||||
88 | /** |
||||||
89 | * Convert an authentication exception into an unauthenticated response. |
||||||
90 | * |
||||||
91 | * @param \Illuminate\Http\Request $request |
||||||
92 | * @param \Illuminate\Auth\AuthenticationException $exception |
||||||
93 | * @return \Illuminate\Http\Response |
||||||
94 | */ |
||||||
95 | protected function unauthenticated($request, AuthenticationException $exception) |
||||||
96 | { |
||||||
97 | if ($request->expectsJson() || $request->isJson()) { |
||||||
98 | return response()->json([ |
||||||
0 ignored issues
–
show
|
|||||||
99 | 'error' => 'Unauthenticated.', |
||||||
100 | 'message' => $exception->getMessage(), |
||||||
101 | ], 401); |
||||||
102 | } |
||||||
103 | |||||||
104 | if (! empty($exception->guards()) && Arr::first($exception->guards()) == 'chief') { |
||||||
105 | return redirect()->guest(route('chief.back.login')); |
||||||
0 ignored issues
–
show
|
|||||||
106 | } |
||||||
107 | |||||||
108 | return redirect()->guest(method_exists($exception, 'redirectTo') ? $exception->redirectTo() : '/'); |
||||||
0 ignored issues
–
show
|
|||||||
109 | } |
||||||
110 | } |
||||||
111 |