|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny; |
|
4
|
|
|
|
|
5
|
|
|
use Jasny\ErrorHandler; |
|
6
|
|
|
use Jasny\ErrorHandler\Middleware; |
|
7
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Handle error in following middlewares/app actions |
|
11
|
|
|
*/ |
|
12
|
|
|
class ErrorHandler implements LoggerAwareInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use ErrorHandler\Logging; |
|
15
|
|
|
use ErrorHandler\ErrorCodes; |
|
16
|
|
|
use ErrorHandler\HandleUncaughtError; |
|
17
|
|
|
use ErrorHandler\HandleShutdownError; |
|
18
|
|
|
use ErrorHandler\HandleUncaughtException; |
|
19
|
|
|
use ErrorHandler\FunctionWrapper; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Exception|\Error |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $error; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var callback |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $onFatalError; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set the caught error |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Throwable|\Exception|\Error |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function setError($error) |
|
39
|
|
|
{ |
|
40
|
4 |
|
if (isset($error) && !$error instanceof \Error && !$error instanceof \Exception) { |
|
41
|
2 |
|
$type = (is_object($error) ? get_class($error) . ' ' : '') . gettype($error); |
|
42
|
2 |
|
trigger_error("Excpeted an Error or Exception, got a $type", E_USER_WARNING); |
|
43
|
2 |
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
$this->error = $error; |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the caught error |
|
51
|
|
|
* |
|
52
|
|
|
* @return \Throwable|\Exception|\Error |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function getError() |
|
55
|
|
|
{ |
|
56
|
4 |
|
return $this->error; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Use this error handler as middleware |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function asMiddleware() |
|
65
|
|
|
{ |
|
66
|
2 |
|
return new Middleware($this); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Log these types of errors or exceptions |
|
72
|
|
|
* |
|
73
|
|
|
* @param int|string $type E_* contants as binary set OR Exception class name |
|
74
|
|
|
*/ |
|
75
|
84 |
|
public function logUncaught($type) |
|
76
|
|
|
{ |
|
77
|
84 |
|
if (is_int($type)) { |
|
78
|
66 |
|
$this->logUncaughtErrors($type); |
|
79
|
52 |
|
} elseif (is_string($type)) { |
|
80
|
18 |
|
$this->logUncaughtException($type); |
|
81
|
9 |
|
} else { |
|
82
|
2 |
|
throw new \InvalidArgumentException("Type should be an error code (int) or Exception class (string)"); |
|
83
|
|
|
} |
|
84
|
82 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set a callback for when the script dies because of a fatal, non-catchable error. |
|
89
|
|
|
* The callback should have an `ErrorException` as only argument. |
|
90
|
|
|
* |
|
91
|
|
|
* @param callable $callback |
|
92
|
|
|
* @param boolean $clearOutput Clear the output buffer before calling the callback |
|
93
|
|
|
*/ |
|
94
|
8 |
|
public function onFatalError($callback, $clearOutput = false) |
|
95
|
|
|
{ |
|
96
|
8 |
|
if (!$clearOutput) { |
|
97
|
4 |
|
$this->onFatalError = $callback; |
|
98
|
2 |
|
} else { |
|
99
|
4 |
|
$this->onFatalError = function ($error) use ($callback) { |
|
100
|
4 |
|
$this->clearOutputBuffer(); |
|
101
|
4 |
|
$callback($error); |
|
102
|
4 |
|
}; |
|
103
|
|
|
} |
|
104
|
8 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Run the fatal error callback |
|
108
|
|
|
* |
|
109
|
|
|
* @param \Exception|\Error $error |
|
110
|
|
|
*/ |
|
111
|
28 |
|
protected function callOnFatalError($error) |
|
112
|
|
|
{ |
|
113
|
28 |
|
if ($this->onFatalError) { |
|
114
|
8 |
|
call_user_func($this->onFatalError, $error); |
|
115
|
4 |
|
} |
|
116
|
28 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Clear and destroy all the output buffers |
|
120
|
|
|
* @codeCoverageIgnore |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function clearOutputBuffer() |
|
123
|
|
|
{ |
|
124
|
|
|
while (ob_get_level() > 0) { |
|
125
|
|
|
ob_end_clean(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|