<?php
class ExceptionHandling {
private static $uncaught_exception = null;
private static $fatal_error = null;
public static function shutdown_callback() {
if (defined('GRACEFUL_SHUTDOWN')) {
// everything went fine
ob_end_flush();
return;
}
header('Status: 500 Internal Server Error');
include(APP_PATH.'/errors/500.php');
}
// turn errors into exceptions
public static function exception_error_handler($errno, $errstr, $errfile, $errline) {
// hhvm can report fatals as errors. We can not convert them to exceptions, however, because they will
// not trigger the exception handler and the stack trace is wrong, anyway. We need to save the errfile and
// errline here because they are not available in the shutdown function.
// for the errno, see https://github.com/facebook/hhvm/blob/master/hphp/runtime/base/runtime-error.h#L57
if ($errno & (1 << 24)) {
self::$fatal_error = array(
'message' => $errstr,
'type' => $errno,
'file' => $errfile,
'line' => $errline
);
}
if (error_reporting() === 0) {
return false; // code used @ to suppress errors
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
public static function exception_handler($exception) {
self::$uncaught_exception = $exception;
}
/**
* @return Exception|null the exception that caused the app to crash, or null if there was none
*/
public static function get_uncaught_exception() {
return self::$uncaught_exception;
}
/**
* Gets the last error that has occurred, be it fatal or non-fatal
* @return array with the fields message, type, file and line
*/
public static function get_last_error() {
if (self::$fatal_error) {
return self::$fatal_error;
}
return error_get_last();
}
}
register_shutdown_function(array('ExceptionHandling', 'shutdown_callback'));
set_error_handler(array('ExceptionHandling', "exception_error_handler"));
set_exception_handler(array('ExceptionHandling', 'exception_handler'));
dklfajelkwa;
Fatal error: Uncaught Error: Undefined constant "APP_PATH" in /in/na982:15
Stack trace:
#0 [internal function]: ExceptionHandling::shutdown_callback()
#1 {main}
thrown in /in/na982 on line 15
Process exited with code 255.
Fatal error: Uncaught ErrorException: Use of undefined constant APP_PATH - assumed 'APP_PATH' (this will throw an Error in a future version of PHP) in /in/na982:15
Stack trace:
#0 /in/na982(15): ExceptionHandling::exception_error_handler(2, 'Use of undefine...', '/in/na982', 15, Array)
#1 [internal function]: ExceptionHandling::shutdown_callback()
#2 {main}
thrown in /in/na982 on line 15
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33
Fatal error: Uncaught ErrorException: Use of undefined constant APP_PATH - assumed 'APP_PATH' in /in/na982:15
Stack trace:
#0 /in/na982(15): ExceptionHandling::exception_error_handler(8, 'Use of undefine...', '/in/na982', 15, Array)
#1 [internal function]: ExceptionHandling::shutdown_callback()
#2 {main}
thrown in /in/na982 on line 15
Process exited with code 255.
Fatal error: Uncaught exception 'ErrorException' with message 'Use of undefined constant APP_PATH - assumed 'APP_PATH'' in /in/na982:15
Stack trace:
#0 /in/na982(15): ExceptionHandling::exception_error_handler(8, 'Use of undefine...', '/in/na982', 15, Array)
#1 [internal function]: ExceptionHandling::shutdown_callback()
#2 {main}
thrown in /in/na982 on line 15
Process exited with code 255.
Output for 5.4.0 - 5.4.6
Fatal error: Uncaught exception 'ErrorException' with message 'Use of undefined constant APP_PATH - assumed 'APP_PATH'' in /in/na982:15
Stack trace:
#0 /in/na982(15): ExceptionHandling::exception_error_handler(8, 'Use of undefine...', '/in/na982', 15, Array)
#1 [internal function]: ExceptionHandling::shutdown_callback()
#2 {main}
thrown in /in/na982 on line 15
Fatal error: Exception thrown without a stack frame in Unknown on line 0
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Class 'ErrorException' not found in /in/na982 on line 38
Notice: Use of undefined constant APP_PATH - assumed 'APP_PATH' in /in/na982 on line 15
Warning: ExceptionHandling::shutdown_callback(APP_PATH/errors/500.php): failed to open stream: No such file or directory in /in/na982 on line 15
Warning: ExceptionHandling::shutdown_callback(): Failed opening 'APP_PATH/errors/500.php' for inclusion (include_path='.:') in /in/na982 on line 15
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/na982 on line 4
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/na982 on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/na982 on line 4
Process exited with code 255.