1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
use Illuminate\Auth\AuthenticationException; |
8
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
9
|
|
|
use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as BugsnagExceptionHandler; |
10
|
|
|
|
11
|
|
|
class Handler extends ExceptionHandler |
12
|
|
|
{ |
13
|
|
|
protected $dontReport = [ |
14
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
15
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
16
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
17
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
18
|
|
|
\Illuminate\Validation\ValidationException::class, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public function report(Exception $e) |
22
|
|
|
{ |
23
|
|
|
parent::report($e); |
24
|
|
|
|
25
|
|
|
if (app()->environment('production') && $this->shouldReport($e)) { |
26
|
|
|
app(BugsnagExceptionHandler::class)->report($e); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function render($request, Exception $e) |
31
|
|
|
{ |
32
|
|
|
if ($this->isHttpException($e)) { |
33
|
|
|
return $this->renderHttpException($e); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->shouldntReport($e)) { |
37
|
|
|
return parent::render($request, $e); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (config('app.debug')) { |
41
|
|
|
return $this->renderExceptionWithWhoops($e); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return parent::render($request, $e); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function renderExceptionWithWhoops(Exception $e): Response |
48
|
|
|
{ |
49
|
|
|
$this->unsetSensitiveData(); |
50
|
|
|
|
51
|
|
|
$whoops = new \Whoops\Run(); |
52
|
|
|
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler()); |
|
|
|
|
53
|
|
|
|
54
|
|
|
return new \Illuminate\Http\Response( |
55
|
|
|
$whoops->handleException($e), |
|
|
|
|
56
|
|
|
$e->getStatusCode(), |
|
|
|
|
57
|
|
|
$e->getHeaders() |
|
|
|
|
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* Don't ever display sensitive data in Whoops pages. |
63
|
|
|
*/ |
64
|
|
|
protected function unsetSensitiveData() |
65
|
|
|
{ |
66
|
|
|
foreach ($_ENV as $key => $value) { |
67
|
|
|
unset($_SERVER[$key]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$_ENV = []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Convert an authentication exception into an unauthenticated response. |
75
|
|
|
* |
76
|
|
|
* @param \Illuminate\Http\Request $request |
77
|
|
|
* @param \Illuminate\Auth\AuthenticationException $e |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Http\Response |
80
|
|
|
*/ |
81
|
|
|
protected function unauthenticated($request, AuthenticationException $e) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if ($request->expectsJson()) { |
84
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return redirect()->guest('login'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.