Created
February 4, 2016 12:17
-
-
Save mstrokin/3c4a1a4b3c82584bbc44 to your computer and use it in GitHub Desktop.
catch exceptions/errors (die on fatal error)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _catch_fatal_error() { | |
$error = error_get_last(); | |
if (($error['type'] === E_ERROR) || ($error['type'] === E_USER_ERROR)|| ($error['type'] === E_USER_NOTICE)) { | |
// fatal error has occured | |
$msg = date("Y-m-d H:i:s")."|FATAL_ERROR: Request:" .$_SERVER['REQUEST_URI']."|". $error['type']. " |Msg : ".$error['message']." |File : ".$error['file']. " |Line : " . $error['line']; | |
echo $msg; | |
die(); | |
} | |
} | |
function _log_error( $num, $str, $file, $line, $context = null ) | |
{ | |
_log_exception( new ErrorException( $str, 0, $num, $file, $line ) ); | |
} | |
function _log_exception( Exception $e ) | |
{ | |
$message = date("Y-m-d H:i:s")."|EXCEPTION: Request:" .$_SERVER['REQUEST_URI']."|Type: " . get_class( $e ) . "; Message: {$e->getMessage()}; File: {$e->getFile()}; Line: {$e->getLine()};"; | |
echo $message; | |
} | |
register_shutdown_function('_catch_fatal_error'); | |
set_error_handler( "_log_error" ); | |
set_exception_handler( "_log_exception"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment