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