1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Auth\AuthenticationException; |
7
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
8
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
9
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
10
|
|
|
use Illuminate\Session\TokenMismatchException; |
11
|
|
|
use Illuminate\Validation\ValidationException; |
12
|
|
|
|
13
|
|
|
class Handler extends ExceptionHandler |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* A list of the exception types that should not be reported. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $dontReport = [ |
21
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
22
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
23
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
24
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
25
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
26
|
|
|
\Illuminate\Validation\ValidationException::class, |
27
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Report or log an exception. |
32
|
|
|
* |
33
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
34
|
|
|
* |
35
|
|
|
* @param \Exception $exception |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
12 |
|
public function report(Exception $exception) |
40
|
|
|
{ |
41
|
12 |
|
parent::report($exception); |
42
|
12 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Render an exception into an HTTP response. |
46
|
|
|
* |
47
|
|
|
* @param \Illuminate\Http\Request $request |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
|
|
public function render($request, Exception $e) |
52
|
|
|
{ |
53
|
|
|
// the below code is for Whoops support. Since Whoops can open some security holes we want to only have it |
54
|
|
|
// enabled in the debug environment. We also don't want Whoops to handle 404 and Validation related exceptions. |
55
|
|
|
if (config('app.debug') && ! ($e instanceof ValidationException) && ! ($e instanceof HttpResponseException)) { |
56
|
|
|
return $this->renderExceptionWithWhoops($e); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// this line allows you to redirect to a route or even back to the current page |
60
|
|
|
// if there is a CSRF Token Mismatch |
61
|
|
|
if ($e instanceof TokenMismatchException) { |
62
|
|
|
return redirect()->route('homepage'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// let's add some support if a Model is not found |
66
|
|
|
// for example, if you were to run a query for User #10000 and that user didn't exist we can return a 404 error |
67
|
|
|
if ($e instanceof ModelNotFoundException) { |
68
|
|
|
return response()->view('errors.404', [], 404); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Let's return a default error page instead of the ugly Laravel error page when we have fatal exceptions |
72
|
|
|
if ($e instanceof \Symfony\Component\Debug\Exception\FatalErrorException) { |
73
|
|
|
return response()->view('errors.500', [], 500); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// finally we are back to the original default error handling provided by Laravel |
77
|
|
|
if ($this->isHttpException($e)) { |
78
|
|
|
switch ($e->getStatusCode()) { |
|
|
|
|
79
|
|
|
// not found |
80
|
|
|
case 404: |
81
|
|
|
return response()->view('errors.404', [], 404); |
82
|
|
|
break; |
|
|
|
|
83
|
|
|
// internal error |
84
|
|
|
case 500: |
85
|
|
|
return response()->view('errors.500', [], 500); |
86
|
|
|
break; |
|
|
|
|
87
|
|
|
|
88
|
|
|
default: |
89
|
|
|
return $this->renderHttpException($e); |
|
|
|
|
90
|
|
|
break; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
return parent::render($request, $e); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Render an exception using Whoops. |
99
|
|
|
* |
100
|
|
|
* @param \Exception $e |
101
|
|
|
* |
102
|
|
|
* @return \Illuminate\Http\Response |
103
|
|
|
*/ |
104
|
|
|
protected function renderExceptionWithWhoops(Exception $e) |
105
|
|
|
{ |
106
|
|
|
if (config('app.debug')) { |
107
|
|
|
$whoops = new \Whoops\Run(); |
108
|
|
|
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler()); |
|
|
|
|
109
|
|
|
|
110
|
|
|
return new \Illuminate\Http\Response( |
111
|
|
|
$whoops->handleException($e), |
|
|
|
|
112
|
|
|
$e->getStatusCode(), |
|
|
|
|
113
|
|
|
$e->getHeaders() |
|
|
|
|
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Convert an authentication exception into an unauthenticated response. |
120
|
|
|
* |
121
|
|
|
* @param \Illuminate\Http\Request $request |
122
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
123
|
|
|
* |
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
|
|
|
return redirect()->guest(route('login')); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.