<?php
$errorTypes = [
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parse Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_USER_DEPRECATED => 'Deprecated',
E_STRICT => 'Strict Standards',
E_RECOVERABLE_ERROR => 'Catchable Fatal'
];
$fatalErrorTypes = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR];
function log_error($num, $str, $file, $line, $context = null, $exit = true) {
global $errorTypes;
global $fatalErrorTypes;
$bFatal = true;
if (isset($errorTypes[$num])) {
$errType = $errorTypes[$num];
$bFatal = in_array($num, $fatalErrorTypes);
} else {
$errType = "Unknown error #$num";
}
// would normally record stuff here
echo "PHP $errType [$file:$line]: $str\n";
if ($bFatal && $exit) {
die;
}
}
function check_for_fatal() {
global $fatalErrorTypes;
$error = error_get_last();
if (in_array($error['type'], $fatalErrorTypes)) {
log_error($error["type"], $error["message"], $error["file"], $error["line"], null, false);
$output = '{"errors":[{"code":"internal_server_error"}]}';
// would normally record stuff here
echo $output;
}
}
function log_exception(\Exception $e) {
echo get_class($e) . ': ' . $e->getMessage();
}
register_shutdown_function('check_for_fatal');
set_error_handler('log_error');
set_exception_handler('log_exception');
$broken = null;
echo $broken->length();
preferences:
27.06 ms | 402 KiB | 5 Q