1
|
|
|
<?php namespace Anomaly\Streams\Platform\Exception; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
use GrahamCampbell\Exceptions\NewExceptionHandler; |
5
|
|
|
use Illuminate\Auth\AuthenticationException; |
6
|
|
|
use Illuminate\Http\RedirectResponse; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Http\Response; |
9
|
|
|
use Illuminate\Validation\ValidationException; |
10
|
|
|
use Symfony\Component\Debug\ExceptionHandler as SymfonyDisplayer; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ExceptionHandler |
15
|
|
|
* |
16
|
|
|
* @link http://pyrocms.com/ |
17
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
18
|
|
|
* @author Ryan Thompson <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ExceptionHandler extends NewExceptionHandler |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A list of the exception types that should not be reported. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $dontReport = [ |
29
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
30
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
31
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
32
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
33
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
34
|
|
|
\Illuminate\Validation\ValidationException::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Render an exception into an HTTP response. |
39
|
|
|
* |
40
|
|
|
* @param Request $request |
41
|
|
|
* @param Exception $e |
42
|
|
|
* @return Response |
43
|
|
|
*/ |
44
|
|
|
public function render($request, Exception $e) |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* Have to catch this for some reason. |
48
|
|
|
* Not sure why our handler passes this. |
49
|
|
|
* |
50
|
|
|
* @todo: Clean up |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
if ($e instanceof AuthenticationException) { |
|
|
|
|
53
|
|
|
if ($request->segment(1) === 'admin') { |
54
|
|
|
return redirect()->guest('admin/login'); |
|
|
|
|
55
|
|
|
} else { |
56
|
|
|
return redirect()->guest('login'); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($e instanceof HttpException) { |
61
|
|
|
if (!$e->getStatusCode() == 404) { |
62
|
|
|
return $this->renderHttpException($e); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (($redirect = config('streams::404.redirect')) && $request->path() !== $redirect) { |
66
|
|
|
return redirect($redirect, 301); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->renderHttpException($e); |
70
|
|
|
} elseif (!config('app.debug') && !$e instanceof ValidationException) { // Maybe shouldntRender here? |
71
|
|
|
return response()->view("streams::errors.500", ['message' => $e->getMessage()], 500); |
72
|
|
|
} else { |
73
|
|
|
return parent::render($request, $e); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Render the given HttpException. |
79
|
|
|
* |
80
|
|
|
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e |
81
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
82
|
|
|
*/ |
83
|
|
|
protected function renderHttpException(HttpException $e) |
84
|
|
|
{ |
85
|
|
|
$status = $e->getStatusCode(); |
86
|
|
|
|
87
|
|
|
if (!config('app.debug') && view()->exists("streams::errors.{$status}")) { |
|
|
|
|
88
|
|
|
return response()->view("streams::errors.{$status}", ['message' => $e->getMessage()], $status); |
89
|
|
|
} else { |
90
|
|
|
return (new SymfonyDisplayer(config('app.debug')))->handle($e); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Map exception into an illuminate response. |
96
|
|
|
* |
97
|
|
|
* @param \Symfony\Component\HttpFoundation\Response $response |
98
|
|
|
* @param \Exception $e |
99
|
|
|
* @return \Illuminate\Http\Response |
100
|
|
|
*/ |
101
|
|
|
protected function toIlluminateResponse($response, Exception $e) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if ($response instanceof SymfonyRedirectResponse) { |
|
|
|
|
104
|
|
|
$response = new RedirectResponse( |
105
|
|
|
$response->getTargetUrl(), |
106
|
|
|
$response->getStatusCode(), |
107
|
|
|
$response->headers->all() |
108
|
|
|
); |
109
|
|
|
} else { |
110
|
|
|
$response = new Response($response->getContent(), $response->getStatusCode(), $response->headers->all()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Have to catch this for some reason. |
115
|
|
|
* Not sure why our handler passes this. |
116
|
|
|
* |
117
|
|
|
* @todo: Clean up |
118
|
|
|
*/ |
119
|
|
|
if ($e instanceof AuthenticationException) { |
120
|
|
|
|
121
|
|
|
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
122
|
|
|
$segments = array_filter(explode('/', $path)); |
123
|
|
|
|
124
|
|
|
if (array_shift($segments) === 'admin') { |
125
|
|
|
return redirect()->guest('admin/login'); |
|
|
|
|
126
|
|
|
} else { |
127
|
|
|
return redirect()->guest('login'); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $response->withException($e); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Convert an authentication exception into an unauthenticated response. |
136
|
|
|
* |
137
|
|
|
* @param \Illuminate\Http\Request $request |
138
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
139
|
|
|
* @return \Illuminate\Http\Response |
140
|
|
|
*/ |
141
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
if ($request->expectsJson()) { |
144
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
View Code Duplication |
if ($request->segment(1) === 'admin') { |
|
|
|
|
148
|
|
|
return redirect()->guest('admin/login'); |
149
|
|
|
} else { |
150
|
|
|
return redirect()->guest('login'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.