|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
|
7
|
|
|
|
|
8
|
|
|
class Handler extends ExceptionHandler |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* A list of the exception types that are not reported. |
|
12
|
|
|
* |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $dontReport = [ |
|
16
|
|
|
\League\OAuth2\Server\Exception\OAuthServerException::class, |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A list of the inputs that are never flashed for validation exceptions. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $dontFlash = [ |
|
25
|
|
|
'password', |
|
26
|
|
|
'password_confirmation', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Report or log an exception. |
|
31
|
|
|
* |
|
32
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Exception $exception |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function report(Exception $exception) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::report($exception); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Render an exception into an HTTP response. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \Illuminate\Http\Request $request |
|
46
|
|
|
* @param \Exception $exception |
|
47
|
|
|
* @return \Illuminate\Http\Response |
|
48
|
|
|
*/ |
|
49
|
|
|
public function render($request, Exception $exception) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($request->wantsJson()) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($exception instanceof \Illuminate\Auth\AuthenticationException) |
|
54
|
|
|
{ |
|
55
|
|
|
\ErrorHandler::unAuthorized(); |
|
56
|
|
|
} |
|
57
|
|
|
if ($exception instanceof \Illuminate\Database\QueryException) |
|
58
|
|
|
{ |
|
59
|
|
|
\ErrorHandler::dbQueryError(); |
|
60
|
|
|
} |
|
61
|
|
|
else if ($exception instanceof \predis\connection\connectionexception) |
|
62
|
|
|
{ |
|
63
|
|
|
\ErrorHandler::redisNotRunning(); |
|
64
|
|
|
} |
|
65
|
|
|
else if ($exception instanceof \GuzzleHttp\Exception\ClientException) |
|
66
|
|
|
{ |
|
67
|
|
|
\ErrorHandler::connectionError(); |
|
68
|
|
|
} |
|
69
|
|
|
else if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) |
|
70
|
|
|
{ |
|
71
|
|
|
return \Response::json($exception->getMessage(), $exception->getStatusCode()); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
else if ($exception instanceof \Illuminate\Validation\ValidationException) |
|
74
|
|
|
{ |
|
75
|
|
|
return \Response::json($exception->errors(), 422); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
else if ( ! $exception instanceof \Symfony\Component\Debug\Exception\FatalErrorException) |
|
78
|
|
|
{ |
|
79
|
|
|
return parent::render($request, $exception); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
else |
|
83
|
|
|
{ |
|
84
|
|
|
return parent::render($request, $exception); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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.