-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorhandler.php
More file actions
54 lines (46 loc) · 1.41 KB
/
errorhandler.php
File metadata and controls
54 lines (46 loc) · 1.41 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
<?php
use Core\Controller;
/**
* Converts the debug backtrace into a string format for debugging.
*
* @return string
*/
function getDebugBacktraceAsString()
{
$backtrace = debug_backtrace();
$traceString = "";
foreach ($backtrace as $index => $trace) {
$file = $trace['file'] ?? '[internal function]';
$line = $trace['line'] ?? '-';
$function = $trace['function'] ?? '[unknown function]';
$class = $trace['class'] ?? '';
$type = $trace['type'] ?? '';
$traceString .= "#{$index} {$file}({$line}): {$class}{$type}{$function}()\n";
}
return $traceString;
}
// Check PHP version compatibility
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
die('PHP version must be 5.3.0 or higher');
}
// Custom error handler
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
$errorType = match ($errno) {
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
default => 'Error'
};
Controller::error('Debug', [
'errorMessage' => "{$errorType}: $errstr in $errfile on line $errline",
'stackTrace' => getDebugBacktraceAsString(),
]);
exit;
});
// Custom exception handler
set_exception_handler(function ($exception) {
Controller::error('Debug', [
'errorMessage' => "Uncaught Exception: " . $exception->getMessage(),
'stackTrace' => $exception->getTraceAsString(),
]);
exit;
});