This commit is contained in:
William 2022-12-18 14:09:42 +00:00
commit e03e17d293
1 changed files with 17 additions and 23 deletions

View File

@ -21,34 +21,28 @@ abstract class ErrorHandler
public static function register(callable $callback)
{
ob_start();
set_error_handler(self::error(...));
set_exception_handler(self::exception(...));
register_shutdown_function(self::shutdown(...), $callback);
}
private static function error($errno, $errstr, $errfile, $errline)
{
$errstr = htmlspecialchars($errstr);
self::$error_messages[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>";
}
set_error_handler(function($errno, $errstr, $errfile, $errline) {
$errstr = htmlspecialchars($errstr);
self::$error_messages[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>";
});
private static function exception($exception)
{
self::$error_messages[] = "<b>Uncaught Exception:</b> " . $exception;
}
set_exception_handler(function($exception) {
self::$error_messages[] = "<b>Uncaught Exception:</b> " . $exception;
});
private static function shutdown(callable $callback)
{
if (!self::$error_messages) {
return;
}
register_shutdown_function(function() use(&$callback) {
if (!self::$error_messages) {
return;
}
// remove current output to be replaced by error page
ob_end_clean();
// remove current output to be replaced by error page
ob_end_clean();
http_response_code(500);
header_remove();
http_response_code(500);
header_remove();
$callback(self::$error_messages);
$callback(self::$error_messages);
});
}
}