1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\ExceptionHandler; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The exception handler decorator. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class HandlerDecorator implements ExceptionHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The default Laravel exception handler. |
16
|
|
|
* |
17
|
|
|
* @var \Illuminate\Contracts\Debug\ExceptionHandler |
18
|
|
|
*/ |
19
|
|
|
protected $defaultHandler; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The custom handlers repository. |
23
|
|
|
* |
24
|
|
|
* @var \Cerbero\ExceptionHandler\HandlersRepository |
25
|
|
|
*/ |
26
|
|
|
protected $repository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set the dependencies. |
30
|
|
|
* |
31
|
|
|
* @param \Illuminate\Contracts\Debug\ExceptionHandler $defaultHandler |
32
|
|
|
* @param \Cerbero\ExceptionHandler\HandlersRepository $repository |
33
|
|
|
*/ |
34
|
35 |
|
public function __construct(ExceptionHandler $defaultHandler, HandlersRepository $repository) |
35
|
|
|
{ |
36
|
35 |
|
$this->defaultHandler = $defaultHandler; |
37
|
|
|
|
38
|
35 |
|
$this->repository = $repository; |
39
|
35 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Report or log an exception. |
43
|
|
|
* |
44
|
|
|
* @param \Exception $e |
45
|
|
|
* @return mixed |
46
|
|
|
* |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
15 |
|
public function report(Exception $e) |
50
|
|
|
{ |
51
|
15 |
|
foreach ($this->repository->getReportersByException($e) as $reporter) { |
52
|
9 |
|
if ($report = $reporter($e)) { |
53
|
9 |
|
return $report; |
54
|
|
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
6 |
|
$this->defaultHandler->report($e); |
58
|
6 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Register a custom handler to report exceptions |
62
|
|
|
* |
63
|
|
|
* @param callable $callback |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
12 |
|
public function reporter(callable $reporter) |
67
|
|
|
{ |
68
|
12 |
|
return $this->repository->addReporter($reporter); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Render an exception into a response. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Http\Request $request |
75
|
|
|
* @param \Exception $e |
76
|
|
|
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response |
77
|
|
|
*/ |
78
|
6 |
|
public function render($request, Exception $e) |
79
|
|
|
{ |
80
|
6 |
|
foreach ($this->repository->getRenderersByException($e) as $renderer) { |
81
|
3 |
|
if ($render = $renderer($e, $request)) { |
82
|
3 |
|
return $render; |
83
|
|
|
} |
84
|
1 |
|
} |
85
|
|
|
|
86
|
3 |
|
return $this->defaultHandler->render($request, $e); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Register a custom handler to render exceptions |
91
|
|
|
* |
92
|
|
|
* @param callable $callback |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
3 |
|
public function renderer(callable $renderer) |
96
|
|
|
{ |
97
|
3 |
|
return $this->repository->addRenderer($renderer); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Render an exception to the console. |
102
|
|
|
* |
103
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
104
|
|
|
* @param \Exception $e |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
6 |
|
public function renderForConsole($output, Exception $e) |
108
|
|
|
{ |
109
|
6 |
|
foreach ($this->repository->getConsoleRenderersByException($e) as $renderer) { |
110
|
3 |
|
if ($render = $renderer($e, $output)) { |
111
|
3 |
|
return $render; |
112
|
|
|
} |
113
|
1 |
|
} |
114
|
|
|
|
115
|
3 |
|
$this->defaultHandler->renderForConsole($output, $e); |
116
|
3 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Register a custom handler to render exceptions in console |
120
|
|
|
* |
121
|
|
|
* @param callable $callback |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
3 |
|
public function consoleRenderer(callable $renderer) |
125
|
|
|
{ |
126
|
3 |
|
return $this->repository->addConsoleRenderer($renderer); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Determine if the exception should be reported. |
131
|
|
|
* |
132
|
|
|
* @param \Exception $e |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
5 |
|
public function shouldReport(Exception $e) |
136
|
|
|
{ |
137
|
5 |
|
if (method_exists($this->defaultHandler, 'shouldReport')) { |
138
|
1 |
|
return $this->defaultHandler->shouldReport($e); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Proxy other calls to default Laravel exception handler |
146
|
|
|
* |
147
|
|
|
* @param string $name |
148
|
|
|
* @param array $parameters |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
3 |
|
public function __call($name, $parameters) |
152
|
|
|
{ |
153
|
3 |
|
return call_user_func_array([$this->defaultHandler, $name], $parameters); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|