improved error handler

This commit is contained in:
Steffen Schröder 2014-05-02 05:12:47 +02:00
parent fb079f6f2e
commit 5f09a4ad3d

View File

@ -110,23 +110,26 @@ class ErrorHandler {
* @return bool * @return bool
*/ */
public function errorHandler($errorNumber, $errorString, $errorFile = null, $errorLine = -1) { public function errorHandler($errorNumber, $errorString, $errorFile = null, $errorLine = -1) {
if (error_reporting() == 0) { if (error_reporting() === 0) {
// Error suppressed // Error suppressed
return false; return false;
} }
$userError = ($errorNumber == E_USER_ERROR || $errorNumber == E_USER_WARNING || $errorNumber == E_USER_NOTICE || $errorNumber == E_USER_DEPRECATED); $userError = $this->isUserErrorNumber($errorNumber);
// Log error // Log error
$errorTag = $this->getErrorTag($errorNumber); $errorTag = $this->getErrorTag($errorNumber);
$message = $errorTag . ': ' . $errorString; $message = $errorTag . ': ' . $errorString;
$fileLine = $errorFile . ': ' . $errorLine;
$traceMessage = $this->parseBackTrace(debug_backtrace()); $traceMessage = $this->parseBackTrace(debug_backtrace());
logMessage($message . ($userError ? '' : PHP_EOL . $traceMessage)); $logMessage = $message . PHP_EOL . ($userError ? $fileLine : $traceMessage);
logMessage($logMessage);
if ($this->reportErrors && !$userError) { if ($this->reportErrors && !$userError) {
$error = array(); $error = array();
$error["Type"] = "Error"; $error["Type"] = "Error";
$error["Message"] = $message; $error["Message"] = $message;
$error["FileLine"] = $fileLine;
$error["Backtrace"] = $traceMessage; $error["Backtrace"] = $traceMessage;
$error['OperatingSystem'] = php_uname(); $error['OperatingSystem'] = php_uname();
$error['PHPVersion'] = phpversion(); $error['PHPVersion'] = phpversion();
@ -140,7 +143,7 @@ class ErrorHandler {
if ($this->maniaControl->settingManager && $this->maniaControl->updateManager) { if ($this->maniaControl->settingManager && $this->maniaControl->updateManager) {
$error['UpdateChannel'] = $this->maniaControl->settingManager->getSetting($this->maniaControl->updateManager, UpdateManager::SETTING_UPDATECHECK_CHANNEL); $error['UpdateChannel'] = $this->maniaControl->settingManager->getSetting($this->maniaControl->updateManager, UpdateManager::SETTING_UPDATECHECK_CHANNEL);
$error['ManiaControlVersion'] = $this->maniaControl->updateManager->getCurrentBuildDate(); $error['ManiaControlVersion'] = ManiaControl::VERSION . '#' . $this->maniaControl->updateManager->getNightlyBuildDate();
} }
else { else {
$error['UpdateChannel'] = ''; $error['UpdateChannel'] = '';
@ -160,14 +163,23 @@ class ErrorHandler {
logMessage("Error successfully reported!"); logMessage("Error successfully reported!");
} }
} }
if ($this->shouldStopExecution($errorNumber)) {
if ($errorNumber == E_ERROR || $errorNumber == E_USER_ERROR || $errorNumber == E_FATAL) {
logMessage('Stopping execution...'); logMessage('Stopping execution...');
exit(); exit();
} }
return false; return false;
} }
/**
* Check if the given Error Number is a User Error
*
* @param int $errorNumber
* @return bool
*/
private function isUserErrorNumber($errorNumber) {
return ($errorNumber === E_USER_ERROR || $errorNumber === E_USER_WARNING || $errorNumber === E_USER_NOTICE || $errorNumber === E_USER_DEPRECATED);
}
/** /**
* Triggers a Debug Notice to the ManiaControl Website * Triggers a Debug Notice to the ManiaControl Website
* *
@ -178,7 +190,7 @@ class ErrorHandler {
} }
/** /**
* Get the prefix for the given error level * Get the Prefix for the given Error Level
* *
* @param int $errorLevel * @param int $errorLevel
* @return string * @return string
@ -217,6 +229,16 @@ class ErrorHandler {
return "[PHP {$errorLevel}]"; return "[PHP {$errorLevel}]";
} }
/**
* Test if ManiaControl should stop its Execution
*
* @param int $errorNumber
* @return bool
*/
private function shouldStopExecution($errorNumber) {
return ($errorNumber === E_ERROR || $errorNumber === E_USER_ERROR || $errorNumber === E_FATAL);
}
/** /**
* Test if ManiaControl should restart automatically * Test if ManiaControl should restart automatically
* *