|
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 Symfony\Component\Debug\ExceptionHandler as SymfonyDisplayer; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ExceptionHandler |
|
14
|
|
|
* |
|
15
|
|
|
* @link http://pyrocms.com/ |
|
16
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
|
17
|
|
|
* @author Ryan Thompson <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ExceptionHandler extends NewExceptionHandler |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A list of the exception types that should not be reported. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $dontReport = [ |
|
28
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
|
29
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
|
30
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
|
31
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
|
32
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
|
33
|
|
|
\Illuminate\Validation\ValidationException::class, |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Render an exception into an HTTP response. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @param Exception $e |
|
41
|
|
|
* @return Response |
|
42
|
|
|
*/ |
|
43
|
|
|
public function render($request, Exception $e) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($e instanceof HttpException) { |
|
46
|
|
|
if (!$e->getStatusCode() == 404) { |
|
47
|
|
|
return $this->renderHttpException($e); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (($redirect = config('streams::404.redirect')) && $request->path() !== $redirect) { |
|
51
|
|
|
return redirect($redirect, 301); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->renderHttpException($e); |
|
55
|
|
|
} elseif (!config('app.debug')) { |
|
56
|
|
|
return response()->view("streams::errors.500", ['message' => $e->getMessage()], 500); |
|
57
|
|
|
} else { |
|
58
|
|
|
return parent::render($request, $e); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Render the given HttpException. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e |
|
66
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function renderHttpException(HttpException $e) |
|
69
|
|
|
{ |
|
70
|
|
|
$status = $e->getStatusCode(); |
|
71
|
|
|
|
|
72
|
|
|
if (!config('app.debug') && view()->exists("streams::errors.{$status}")) { |
|
|
|
|
|
|
73
|
|
|
return response()->view("streams::errors.{$status}", ['message' => $e->getMessage()], $status); |
|
74
|
|
|
} else { |
|
75
|
|
|
return (new SymfonyDisplayer(config('app.debug')))->handle($e); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Map exception into an illuminate response. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Symfony\Component\HttpFoundation\Response $response |
|
83
|
|
|
* @param \Exception $e |
|
84
|
|
|
* @return \Illuminate\Http\Response |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function toIlluminateResponse($response, Exception $e) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
if ($response instanceof SymfonyRedirectResponse) { |
|
|
|
|
|
|
89
|
|
|
$response = new RedirectResponse( |
|
90
|
|
|
$response->getTargetUrl(), |
|
91
|
|
|
$response->getStatusCode(), |
|
92
|
|
|
$response->headers->all() |
|
93
|
|
|
); |
|
94
|
|
|
} else { |
|
95
|
|
|
$response = new Response($response->getContent(), $response->getStatusCode(), $response->headers->all()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Have to catch this for some reason. |
|
100
|
|
|
* Not sure why our handler passes this. |
|
101
|
|
|
* |
|
102
|
|
|
* @todo: Clean up |
|
103
|
|
|
*/ |
|
104
|
|
|
if ($e instanceof AuthenticationException) { |
|
105
|
|
|
|
|
106
|
|
|
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
|
107
|
|
|
$segments = array_filter(explode('/', $path)); |
|
108
|
|
|
|
|
109
|
|
|
if (array_shift($segments) === 'admin') { |
|
110
|
|
|
return redirect()->guest('admin/login'); |
|
|
|
|
|
|
111
|
|
|
} else { |
|
112
|
|
|
return redirect()->guest('login'); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $response->withException($e); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Convert an authentication exception into an unauthenticated response. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Illuminate\Http\Request $request |
|
123
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
|
124
|
|
|
* @return \Illuminate\Http\Response |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
if ($request->expectsJson()) { |
|
129
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($request->segment(1) === 'admin') { |
|
133
|
|
|
return redirect()->guest('admin/login'); |
|
134
|
|
|
} else { |
|
135
|
|
|
return redirect()->guest('login'); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.