|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WebServCo\Framework; |
|
6
|
|
|
|
|
7
|
|
|
final class ErrorHandler |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Restores default error handler. |
|
11
|
|
|
*/ |
|
12
|
|
|
public static function restore(): bool |
|
13
|
|
|
{ |
|
14
|
|
|
return \restore_error_handler(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Registers as the error handler. |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function set(): bool |
|
21
|
|
|
{ |
|
22
|
|
|
\ini_set('display_errors', '0'); |
|
23
|
|
|
\set_error_handler(['\WebServCo\Framework\ErrorHandler', 'throwErrorException']); |
|
24
|
|
|
return true; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Throw ErrorException. |
|
29
|
|
|
* |
|
30
|
|
|
* @param int $errno Error level |
|
31
|
|
|
* @param string $errstr Error message |
|
32
|
|
|
* @param string $errfile Filename the error was raised in |
|
33
|
|
|
* @param int $errline Line number the error was raised at |
|
34
|
|
|
* @throws \ErrorException |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function throwErrorException(int $errno, string $errstr, string $errfile, int $errline): bool |
|
37
|
|
|
{ |
|
38
|
|
|
/* Handle error reporting disabled or supressed * |
|
39
|
|
|
// CURRENT SITUATION: ignore error reporting disabled or supressed, handle all errors |
|
40
|
|
|
// Custom error handler is called even if errors disable or supressed (@) |
|
41
|
|
|
// Code below handles this. |
|
42
|
|
|
// https://www.php.net/manual/en/language.operators.errorcontrol.php |
|
43
|
|
|
// https://www.php.net/manual/en/function.set-error-handler.php |
|
44
|
|
|
// https://www.php.net/manual/en/migration80.incompatible.php |
|
45
|
|
|
if (!(\error_reporting() & $errno)) { // bitwise operator, not a typo |
|
46
|
|
|
// This error code is not included in error_reporting, so let it fall |
|
47
|
|
|
// through to the standard PHP error handler |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
/* Handle error reporting disabled or supressed */ |
|
51
|
|
|
|
|
52
|
|
|
// Make sure \error_get_last will not report this again (used in ErrorObjectHelper::get()) |
|
53
|
|
|
\error_clear_last(); |
|
54
|
|
|
|
|
55
|
|
|
throw new \ErrorException( |
|
56
|
|
|
\sprintf('%s: %s.', \WebServCo\Framework\Helpers\ErrorTypeHelper::getString($errno), $errstr), // message |
|
57
|
|
|
0, // code |
|
58
|
|
|
$errno, // severity |
|
59
|
|
|
$errfile, // filename |
|
60
|
|
|
$errline, // lineno |
|
61
|
|
|
null, // previous |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|