-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.php
More file actions
96 lines (83 loc) · 3.06 KB
/
ErrorHandler.php
File metadata and controls
96 lines (83 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
namespace SetBased\ErrorHandler;
use SetBased\Exception\ErrorException;
/**
* A class for handling PHP runtime errors and warnings in a uniform manner by throwing exceptions.
*/
class ErrorHandler
{
//--------------------------------------------------------------------------------------------------------------------
/**
* An error handler function that throws exceptions.
*
* @param int $errno The level of the error raised.
* @param string $errstr The error message.
* @param string|null $errfile The filename that the error was raised in.
* @param int|null $errline The line number the error was raised at.
*
* @return bool
*
* See http://php.net/manual/en/function.set-error-handler.php.
*
* @throws ErrorException
*/
public function handleError(int $errno, string $errstr, ?string $errfile, ?int $errline): bool
{
// See https://www.php.net/manual/en/language.operators.errorcontrol.php for the bitwise or expression.
if (error_reporting()===( E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE))
{
// The error was suppressed with the @-operator. Don't throw an exception.
return false;
}
$exception = new ErrorException($errstr, $errno, $errno, $errfile, $errline);
// In case error appeared in __toString method we can't throw any exception.
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($trace);
foreach ($trace as $frame)
{
if ($frame['function']==='__toString')
{
$handler = set_exception_handler(null);
restore_exception_handler();
if (is_callable($handler))
{
$handler($exception);
}
else
{
error_log((string)$exception);
}
// If running unit tests return true.
if (defined('PHPUNIT_ERROR_HANDLER_TEST_SUITE'))
{
return true;
}
exit(-1);
}
}
throw $exception;
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Registers this error handler.
*
* @param null|int $errorTypes The mask for triggering this error handler. Defaults to E_ALL. Note E_STRICT is part
* of E_ALL since PHP 5.4.0.
*/
public function registerErrorHandler(?int $errorTypes = E_ALL): void
{
ini_set('display_errors', '0');
set_error_handler([$this, 'handleError'], $errorTypes);
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Unregisters this error handler by restoring the PHP error and exception handlers.
*/
public function unregisterErrorHandler(): void
{
restore_error_handler();
}
//--------------------------------------------------------------------------------------------------------------------
}
//----------------------------------------------------------------------------------------------------------------------