1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\ErrorHandler; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait for handling uncaught exceptions using PHP's exception handler |
7
|
|
|
*/ |
8
|
|
|
trait HandleUncaughtException |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var callable|false |
12
|
|
|
*/ |
13
|
|
|
protected $chainedExceptionHandler; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Log the following exception classes (and subclasses) |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $logExceptionClasses = []; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Wrapper method for `set_error_handler` |
25
|
|
|
* |
26
|
|
|
* @param callable $callback |
27
|
|
|
* @param int $error_types |
28
|
|
|
* @return callable|null |
29
|
|
|
*/ |
30
|
|
|
abstract protected function setErrorHandler($callback, $error_types = E_ALL); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Wrapper method for `set_exception_handler` |
34
|
|
|
* |
35
|
|
|
* @param callable $callback |
36
|
|
|
* @return callable|null |
37
|
|
|
*/ |
38
|
|
|
abstract protected function setExceptionHandler($callback); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Log an error or exception |
42
|
|
|
* |
43
|
|
|
* @param \Exception|\Error $error |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
abstract public function log($error); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the types of errors that will be logged |
50
|
|
|
* |
51
|
|
|
* @return int Binary set of E_* constants |
52
|
|
|
*/ |
53
|
|
|
abstract public function getLoggedErrorTypes(); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the error handler that has been replaced. |
58
|
|
|
* |
59
|
|
|
* @return callable|false|null |
60
|
|
|
*/ |
61
|
2 |
|
public function getChainedExceptionHandler() |
62
|
|
|
{ |
63
|
2 |
|
return $this->chainedExceptionHandler; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get a list of Exception and other Throwable classes that will be logged |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
4 |
|
public function getLoggedExceptionClasses() |
71
|
|
|
{ |
72
|
4 |
|
return $this->logExceptionClasses; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Log these types of errors or exceptions |
78
|
|
|
* |
79
|
|
|
* @param string $class Exception class name |
80
|
|
|
*/ |
81
|
18 |
|
protected function logUncaughtException($class) |
82
|
|
|
{ |
83
|
18 |
|
if (!in_array($class, $this->logExceptionClasses)) { |
84
|
18 |
|
$this->logExceptionClasses[] = $class; |
85
|
|
|
} |
86
|
|
|
|
87
|
18 |
|
$this->initExceptionHandler(); |
88
|
18 |
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Use the global error handler |
93
|
|
|
*/ |
94
|
18 |
|
protected function initExceptionHandler() |
95
|
|
|
{ |
96
|
18 |
|
if (!isset($this->chainedExceptionHandler)) { |
97
|
18 |
|
$this->chainedExceptionHandler = $this->setExceptionHandler([$this, 'handleException']) ?: false; |
98
|
|
|
} |
99
|
18 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Uncaught exception handler |
103
|
|
|
* @ignore |
104
|
|
|
* |
105
|
|
|
* @param \Exception|\Error $exception |
106
|
|
|
*/ |
107
|
18 |
|
public function handleException($exception) |
108
|
|
|
{ |
109
|
18 |
|
$this->setExceptionHandler(null); |
|
|
|
|
110
|
18 |
|
$this->setErrorHandler(null); |
|
|
|
|
111
|
|
|
|
112
|
18 |
|
$isInstanceOf = array_map(function ($class) use ($exception) { |
113
|
12 |
|
return is_a($exception, $class); |
114
|
18 |
|
}, $this->logExceptionClasses); |
115
|
|
|
|
116
|
18 |
|
if ($exception instanceof \Error || $exception instanceof \ErrorException) { |
117
|
4 |
|
$type = $exception instanceof \Error ? $exception->getCode() : $exception->getSeverity(); |
118
|
4 |
|
$shouldLog = $this->getLoggedErrorTypes() & $type; |
119
|
|
|
} else { |
120
|
14 |
|
$shouldLog = array_sum($isInstanceOf) > 0; |
121
|
|
|
} |
122
|
|
|
|
123
|
18 |
|
if ($shouldLog) { |
124
|
12 |
|
$this->log($exception); |
125
|
|
|
} |
126
|
|
|
|
127
|
18 |
|
$this->callOnFatalError($exception); |
|
|
|
|
128
|
|
|
|
129
|
18 |
|
if ($this->chainedExceptionHandler) { |
130
|
6 |
|
call_user_func($this->chainedExceptionHandler, $exception); |
131
|
|
|
} |
132
|
|
|
|
133
|
18 |
|
throw $exception; // This is now handled by the default exception and error handler |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: