1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
7
|
|
|
use Illuminate\Auth\AuthenticationException; |
8
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract; |
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
12
|
|
|
|
13
|
|
|
class Handler implements ExceptionHandlerContract |
14
|
|
|
{ |
15
|
|
|
protected $parentHandler; |
16
|
|
|
|
17
|
|
|
public function __construct(ExceptionHandlerContract $parentHandler) |
18
|
|
|
{ |
19
|
|
|
$this->parentHandler = $parentHandler; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Report or log an exception. |
24
|
|
|
* |
25
|
|
|
* @param \Exception $exception |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function report(Exception $exception) |
31
|
|
|
{ |
32
|
|
|
$this->parentHandler->report($exception); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Render an exception into a response. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Http\Request $request |
39
|
|
|
* @param \Exception $exception |
40
|
|
|
* |
41
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
42
|
|
|
*/ |
43
|
|
|
public function render($request, Exception $exception) |
44
|
|
|
{ |
45
|
|
|
$exception = $this->prepareException($exception); |
46
|
|
|
|
47
|
|
|
if ($exception instanceof AuthenticationException) { |
48
|
|
|
$response = $this->unauthenticated($request, $exception); |
49
|
|
|
} elseif ($exception instanceof HttpException) { |
50
|
|
|
$response = $this->renderHttpException($request, $exception); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($response) { |
54
|
|
|
return $response; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->parentHandler->render($request, $exception); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Render an exception to the console. |
62
|
|
|
* |
63
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
64
|
|
|
* @param \Exception $exception |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function renderForConsole($output, Exception $exception) |
69
|
|
|
{ |
70
|
|
|
$this->parentHandler->renderForConsole($output, $exception); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Prepare exception for rendering. |
75
|
|
|
* |
76
|
|
|
* @param \Exception $exception |
77
|
|
|
* @return \Exception |
78
|
|
|
*/ |
79
|
|
|
protected function prepareException(Exception $exception) |
80
|
|
|
{ |
81
|
|
|
if ($exception instanceof ModelNotFoundException) { |
82
|
|
|
$exception = new NotFoundHttpException($exception->getMessage(), $exception); |
83
|
|
|
} elseif ($exception instanceof AuthorizationException) { |
84
|
|
|
$exception = new HttpException(403, $exception->getMessage() ?: 'Forbidden'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $exception; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Convert an authentication exception into an unauthenticated response. |
92
|
|
|
* |
93
|
|
|
* @param \Illuminate\Http\Request $request |
94
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
95
|
|
|
* @return \Illuminate\Http\Response |
96
|
|
|
*/ |
97
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if ($request->expectsJson()) { |
100
|
|
|
return response('Unauthenticated.', 401); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->isAdmin($request)) { |
104
|
|
|
return redirect()->guest(route('admin.login')); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Render the given HttpException. |
110
|
|
|
* |
111
|
|
|
* @param \Illuminate\Http\Request $request |
112
|
|
|
* @param \Symfony\Component\HttpKernel\Exception\HttpException $exception |
113
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
114
|
|
|
*/ |
115
|
|
|
protected function renderHttpException($request, HttpException $exception) |
116
|
|
|
{ |
117
|
|
|
$status = $exception->getStatusCode(); |
118
|
|
|
if ($request->expectsJson()) { |
119
|
|
|
return response($exception->getMessage() ?: 'Not Found', $status); |
120
|
|
|
} |
121
|
|
|
if ($this->isAdmin($request)) { |
|
|
|
|
122
|
|
|
//return response()->view('admin::app', ['exception' => $exception], $status, $exception->getHeaders()); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function isAdmin($request) |
127
|
|
|
{ |
128
|
|
|
return $request->route() && $request->route()->getPrefix() == 'admin'; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: