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
|
|
|
return $this->unauthenticated($request, $exception); |
49
|
|
|
} elseif ($exception instanceof HttpException) { |
50
|
|
|
return $this->renderHttpException($request, $exception); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->parentHandler->render($request, $exception); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Render an exception to the console. |
58
|
|
|
* |
59
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
60
|
|
|
* @param \Exception $exception |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function renderForConsole($output, Exception $exception) |
65
|
|
|
{ |
66
|
|
|
$this->parentHandler->renderForConsole($output, $exception); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Prepare exception for rendering. |
71
|
|
|
* |
72
|
|
|
* @param \Exception $exception |
73
|
|
|
* @return \Exception |
74
|
|
|
*/ |
75
|
|
|
protected function prepareException(Exception $exception) |
76
|
|
|
{ |
77
|
|
|
if ($exception instanceof ModelNotFoundException) { |
78
|
|
|
$exception = new NotFoundHttpException($exception->getMessage(), $exception); |
79
|
|
|
} elseif ($exception instanceof AuthorizationException) { |
80
|
|
|
$exception = new HttpException(403, $exception->getMessage() ?: 'Forbidden'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $exception; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Convert an authentication exception into an unauthenticated response. |
88
|
|
|
* |
89
|
|
|
* @param \Illuminate\Http\Request $request |
90
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
91
|
|
|
* @return \Illuminate\Http\Response |
92
|
|
|
*/ |
93
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if ($request->expectsJson()) { |
96
|
|
|
return response('Unauthenticated.', 401); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return redirect()->guest(route('admin.login')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Render the given HttpException. |
104
|
|
|
* |
105
|
|
|
* @param \Illuminate\Http\Request $request |
106
|
|
|
* @param \Symfony\Component\HttpKernel\Exception\HttpException $exception |
107
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
108
|
|
|
*/ |
109
|
|
|
protected function renderHttpException($request, HttpException $exception) |
110
|
|
|
{ |
111
|
|
|
$status = $exception->getStatusCode(); |
112
|
|
|
if ($request->expectsJson()) { |
113
|
|
|
return response($exception->getMessage() ?: 'Not Found', $status); |
114
|
|
|
} |
115
|
|
|
return response()->view('admin::app', ['exception' => $exception], $status, $exception->getHeaders()); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.