-
-
Save gajus/18a74a62fee4f2a34bb7 to your computer and use it in GitHub Desktop.
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
/** | |
* @author Gajus Kuizinas <g.kuizinas@anuary.com> | |
* @version 1.0.5 (2012 10 15) | |
*/ | |
function error_exception_handler() | |
{ | |
global $db; | |
$args = func_get_args(); | |
if (func_num_args() == 1) { | |
$data = array | |
( | |
'type' => NULL, | |
'message' => $args[0]->getMessage(), | |
'file' => $args[0]->getFile(), | |
'line' => $args[0]->getLine() | |
); | |
} else { | |
$data = array | |
( | |
'type' => $args[0], | |
'message' => $args[1], | |
'file' => $args[2], | |
'line' => $args[3] | |
); | |
} | |
if (ob_get_level()) { | |
ob_clean(); | |
} | |
if (!headers_sent()) { | |
header('Content-Type: text/plain'); | |
http_response_code(500); | |
} | |
if(DEBUG) { | |
if ($data['type'] === NULL) { | |
$error_type = get_class($args[0]); | |
} else { | |
switch($data['type']) { | |
case E_ERROR: | |
case E_USER_ERROR: | |
$error_type = 'Fatal run-time error.'; | |
break; | |
case E_WARNING: | |
case E_USER_WARNING: | |
$error_type = 'Run-time warnings (non-fatal error).'; | |
break; | |
case E_NOTICE: | |
case E_USER_NOTICE: | |
$error_type = 'Run-time notice.'; | |
break; | |
case ERROR_CSS: | |
$error_type = 'LESS error.'; | |
break; | |
default: | |
$error_type = 'Unknown ' . $data['type'] . '.'; | |
break; | |
} | |
} | |
ob_start(); | |
echo "Type:\t\t{$error_type}\nMessage:\t{$data['message']}\nFile:\t\t{$data['file']}\nLine:\t\t{$data['line']}\nTime:\t\t" . date(FORMAT_DATETIME) . "\n\n"; | |
debug_print_backtrace(); | |
echo str_replace(realpath(PATH_BASE . '/..'), '[project]', ob_get_clean()); | |
} else { | |
echo 'Unexpected system behaviour. The error has been reported to the system administrator.'; | |
} | |
finish_request(); | |
$debug_backtrace = debug_backtrace(); | |
array_shift($debug_backtrace); | |
$data['debug_backtrace'] = json_encode($debug_backtrace); | |
if (!empty($db)) { | |
$db | |
->prepare("INSERT INTO `anuary`.`errors` (`type`, `message`, `file`, `line`, `debug_backtrace`) VALUES (i:type, s:message, s:file, i:line, s:debug_backtrace);") | |
->execute($data); | |
} | |
return FALSE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment