1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
// controller |
6
|
|
|
use Bugsnag; |
7
|
|
|
//use Illuminate\Validation\ValidationException; |
8
|
|
|
use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as ExceptionHandler; |
9
|
|
|
use Exception; |
10
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
11
|
|
|
// use Symfony\Component\HttpKernel\Exception\HttpException; |
12
|
|
|
// use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
13
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
14
|
|
|
use Illuminate\Foundation\Validation\ValidationException; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
|
17
|
|
|
class Handler extends ExceptionHandler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* A list of the exception types that should not be reported. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $dontReport = [ |
25
|
|
|
// 'Symfony\Component\HttpKernel\Exception\HttpException', |
26
|
|
|
\Illuminate\Http\Exception\HttpResponseException::class, |
27
|
|
|
ValidationException::class, |
28
|
|
|
AuthorizationException::class, |
29
|
|
|
HttpResponseException ::class, |
30
|
|
|
ModelNotFoundException::class, |
31
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Report or log an exception. |
36
|
|
|
* |
37
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
38
|
|
|
* |
39
|
|
|
* @param \Exception $e |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function report(Exception $e) |
44
|
|
|
{ |
45
|
|
|
$debug = \Config::get('app.bugsnag_reporting'); |
46
|
|
|
$debug = ($debug) ? 'true' : 'false'; |
47
|
|
|
if ($debug == 'false') { |
48
|
|
|
Bugsnag::setBeforeNotifyFunction(function ($error) { |
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
}); |
51
|
|
|
} else { |
52
|
|
|
$version = \Config::get('app.version'); |
53
|
|
|
Bugsnag::setAppVersion($version); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return parent::report($e); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Render an exception into an HTTP response. |
61
|
|
|
* |
62
|
|
|
* @param type $request |
63
|
|
|
* @param Exception $e |
64
|
|
|
* |
65
|
|
|
* @return type mixed |
66
|
|
|
*/ |
67
|
|
|
public function render($request, Exception $e) |
68
|
|
|
{ |
69
|
|
|
switch ($e) { |
70
|
|
|
case $e instanceof \Illuminate\Http\Exception\HttpResponseException: |
71
|
|
|
return parent::render($request, $e); |
|
|
|
|
72
|
|
View Code Duplication |
case $e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException: |
|
|
|
|
73
|
|
|
return response()->json(['message' => $e->getMessage(), 'code' => $e->getStatusCode()]); |
|
|
|
|
74
|
|
View Code Duplication |
case $e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException: |
|
|
|
|
75
|
|
|
return response()->json(['message' => $e->getMessage(), 'code' => $e->getStatusCode()]); |
|
|
|
|
76
|
|
|
default: |
77
|
|
|
return $this->common($request, $e); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Function to render 500 error page. |
83
|
|
|
* |
84
|
|
|
* @param type $request |
85
|
|
|
* @param type $e |
86
|
|
|
* |
87
|
|
|
* @return type mixed |
88
|
|
|
*/ |
89
|
|
|
public function render500($request, $e) |
90
|
|
|
{ |
91
|
|
|
if (config('app.debug') == true) { |
92
|
|
|
return parent::render($request, $e); |
|
|
|
|
93
|
|
|
} elseif ($e instanceof ValidationException) { |
94
|
|
|
return parent::render($request, $e); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return response()->view('errors.500'); |
98
|
|
|
//return redirect()->route('error500', []); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Function to render 404 error page. |
103
|
|
|
* |
104
|
|
|
* @param type $request |
105
|
|
|
* @param type $e |
106
|
|
|
* |
107
|
|
|
* @return type mixed |
108
|
|
|
*/ |
109
|
|
|
public function render404($request, $e) |
110
|
|
|
{ |
111
|
|
|
$seg = $request->segments(); |
112
|
|
|
if (in_array('api', $seg)) { |
113
|
|
|
return response()->json(['status' => '404']); |
114
|
|
|
} |
115
|
|
|
if (config('app.debug') == true) { |
116
|
|
|
if ($e->getStatusCode() == '404') { |
117
|
|
|
return redirect()->route('error404', []); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return parent::render($request, $e); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return redirect()->route('error404', []); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Function to render database connection failed. |
128
|
|
|
* |
129
|
|
|
* @param type $request |
130
|
|
|
* @param type $e |
131
|
|
|
* |
132
|
|
|
* @return type mixed |
133
|
|
|
*/ |
134
|
|
|
public function renderDB($request, $e) |
135
|
|
|
{ |
136
|
|
|
$seg = $request->segments(); |
137
|
|
|
if (in_array('api', $seg)) { |
138
|
|
|
return response()->json(['status' => '404']); |
139
|
|
|
} |
140
|
|
|
if (config('app.debug') == true) { |
141
|
|
|
return parent::render($request, $e); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return redirect()->route('error404', []); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Common finction to render both types of codes. |
149
|
|
|
* |
150
|
|
|
* @param type $request |
151
|
|
|
* @param type $e |
152
|
|
|
* |
153
|
|
|
* @return type mixed |
154
|
|
|
*/ |
155
|
|
|
public function common($request, $e) |
156
|
|
|
{ |
157
|
|
|
switch ($e) { |
158
|
|
|
case $e instanceof HttpException: |
|
|
|
|
159
|
|
|
return $this->render404($request, $e); |
|
|
|
|
160
|
|
|
case $e instanceof NotFoundHttpException: |
161
|
|
|
return $this->render404($request, $e); |
|
|
|
|
162
|
|
|
case $e instanceof PDOException: |
|
|
|
|
163
|
|
|
if (strpos('1045', $e->getMessage()) == true) { |
|
|
|
|
164
|
|
|
return $this->renderDB($request, $e); |
|
|
|
|
165
|
|
|
} else { |
166
|
|
|
return $this->render500($request, $e); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
// case $e instanceof ErrorException: |
|
|
|
|
169
|
|
|
// if($e->getMessage() == 'Breadcrumb not found with name "" ') { |
170
|
|
|
// return $this->render404($request, $e); |
171
|
|
|
// } else { |
172
|
|
|
// return parent::render($request, $e); |
173
|
|
|
// } |
174
|
|
|
default: |
175
|
|
|
return $this->render500($request, $e); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return parent::render($request, $e); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.