-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnhandledHandler.php
executable file
·117 lines (100 loc) · 2.45 KB
/
UnhandledHandler.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Jingga
*
* PHP Version 8.2
*
* @package phpOMS
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS;
use phpOMS\Log\FileLogger;
/**
* Default exception and error handler.
*
* @package phpOMS
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
final class UnhandledHandler
{
/**
* Exception handler.
*
* @param \Throwable $e Exception
*
* @return void
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public static function exceptionHandler(\Throwable $e) : void
{
$logger = FileLogger::getInstance(__DIR__ . '/../Logs');
$logger->critical(FileLogger::MSG_FULL, [
'message' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
$_SERVER = [];
}
/**
* Error handler.
*
* @param int $errno Error number
* @param string $errstr Error message
* @param string $errfile Error file
* @param int $errline Error line
*
* @return bool Returns true if the error could be logged otherwise false is returned
*
* @since 1.0.0
*/
public static function errorHandler(int $errno, string $errstr, string $errfile, int $errline) : bool
{
$logger = FileLogger::getInstance(__DIR__ . '/../Logs');
if ((\error_reporting() & $errno) === 0) {
\error_clear_last();
$_SERVER = [];
return false;
}
\error_clear_last();
$logger->error(FileLogger::MSG_FULL, [
'message' => 'Undefined error',
'str' => $errstr,
'line' => $errline,
'file' => $errfile,
]);
$_SERVER = [];
return true;
}
/**
* Shutdown handler.
*
* @return void
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public static function shutdownHandler() : void
{
$e = \error_get_last();
\error_clear_last();
if ($e === null) {
$_SERVER = [];
return;
}
$logger = FileLogger::getInstance(__DIR__ . '/../Logs');
$logger->warning(FileLogger::MSG_FULL, [
'message' => $e['message'],
'line' => $e['line'],
'file' => $e['file'],
]);
$_SERVER = [];
}
}