1
|
|
|
<?php namespace App\Exceptions; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
7
|
|
|
use Whoops\Handler\PrettyPageHandler; |
8
|
|
|
use Whoops\Run as RunWhoops; |
9
|
|
|
|
10
|
|
|
class Handler extends ExceptionHandler |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A list of the exception types that should not be reported. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $dontReport = [ |
19
|
|
|
'Symfony\Component\HttpKernel\Exception\HttpException' |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Report or log an exception. |
24
|
|
|
* |
25
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
26
|
|
|
* |
27
|
|
|
* @param \Exception $e |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function report(Exception $e) |
31
|
|
|
{ |
32
|
|
|
return parent::report($e); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Render an exception into an HTTP response. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Http\Request $request |
39
|
|
|
* @param \Exception $e |
40
|
|
|
* @return \Illuminate\Http\Response |
41
|
|
|
*/ |
42
|
|
|
public function render($request, Exception $e) |
43
|
|
|
{ |
44
|
|
|
if ($this->isHttpException($e)) { |
45
|
|
|
return $this->renderHttpException($e); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (config('app.debug')) { |
49
|
|
|
if ($request->isXmlHttpRequest()) { |
50
|
|
|
return response(['message' => $e->getMessage(), |
|
|
|
|
51
|
|
|
'code' => $e->getCode(), |
52
|
|
|
'file' => $e->getFile(), |
53
|
|
|
'line' => $e->getLine()], 500); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->renderExceptionWithWhoops($e); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($e instanceof RepositoryException) { |
60
|
|
|
return $e->jsonResponse(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return parent::render($request, $e); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Render an exception using Whoops. |
68
|
|
|
* |
69
|
|
|
* @param \Exception $e |
70
|
|
|
* @return \Illuminate\Http\Response |
71
|
|
|
*/ |
72
|
|
|
protected function renderExceptionWithWhoops(Exception $e) |
73
|
|
|
{ |
74
|
|
|
$whoops = new RunWhoops(); |
75
|
|
|
$whoops->pushHandler(new PrettyPageHandler()); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if ($e instanceof HttpException) { |
78
|
|
|
return new Response($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new Response($whoops->handleException($e)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
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.