Skip to content

Commit 22a4332

Browse files
committed
Add new ErrorHandlerTrait
1 parent 0fa250a commit 22a4332

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace aleksip\DataTransformPlugin;
4+
5+
trait ErrorHandlerTrait
6+
{
7+
protected static $levels = [
8+
E_WARNING => 'E_WARNING',
9+
E_NOTICE => 'E_NOTICE',
10+
E_DEPRECATED => 'E_DEPRECATED',
11+
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
12+
];
13+
14+
protected $errno;
15+
protected $errstr;
16+
17+
protected function errorHandler($errno, $errstr)
18+
{
19+
$this->errno = $errno;
20+
$this->errstr = $errstr;
21+
22+
return TRUE;
23+
}
24+
25+
protected function setErrorHandler()
26+
{
27+
$this->errno = null;
28+
$this->errstr = null;
29+
set_error_handler([$this, 'errorHandler']);
30+
}
31+
32+
protected function restoreErrorHandler($errorMessage = null)
33+
{
34+
restore_error_handler();
35+
if (isset($this->errno)) {
36+
$level = isset(self::$levels[$this->errno]) ? self::$levels[$this->errno] : $this->errno;
37+
if (isset($errorMessage)) {
38+
DataTransformPlugin::writeWarning($errorMessage);
39+
}
40+
DataTransformPlugin::writeWarning($this->errstr . ' (' . $level . ')', isset($errorMessage));
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)