added exception class to error report

improved code
This commit is contained in:
Steffen Schröder 2014-05-04 00:16:50 +02:00
parent de8905c475
commit e79bb64cbb

View File

@ -16,7 +16,7 @@ class ErrorHandler {
/* /*
* Constants * Constants
*/ */
const MC_DEBUG_NOTICE = "ManiaControl.DebugNotice"; const MC_DEBUG_NOTICE = 'ManiaControl.DebugNotice';
const SETTING_RESTART_ON_EXCEPTION = 'Automatically restart on Exceptions'; const SETTING_RESTART_ON_EXCEPTION = 'Automatically restart on Exceptions';
/* /*
@ -42,23 +42,26 @@ class ErrorHandler {
} }
/** /**
* ManiaControl ExceptionHandler * ManiaControl Exception Handler
* *
* @param \Exception $ex * @param \Exception $ex
* @param bool $shutdown * @param bool $shutdown
*/ */
public function handleException(\Exception $ex, $shutdown = true) { public function handleException(\Exception $ex, $shutdown = true) {
// Log exception
$message = "[ManiaControl EXCEPTION]: {$ex->getMessage()}"; $message = "[ManiaControl EXCEPTION]: {$ex->getMessage()}";
$traceMessage = 'Class: ' . get_class($ex) . PHP_EOL;
$traceMessage .= 'Trace:' . PHP_EOL . $ex->getTraceAsString(); $exceptionClass = get_class($ex);
logMessage($message . PHP_EOL . $traceMessage); $traceString = $ex->getTraceAsString();
$logMessage = $message . PHP_EOL . 'Class: ' . $exceptionClass . PHP_EOL . 'Trace:' . PHP_EOL . $traceString;
logMessage($logMessage);
if ($this->reportErrors) { if ($this->reportErrors) {
$error = array(); $error = array();
$error["Type"] = "Exception"; $error['Type'] = 'Exception';
$error["Message"] = $message; $error['Message'] = $message;
$error["Backtrace"] = $traceMessage; $error['Class'] = $exceptionClass;
$error['Backtrace'] = $traceString;
$error['OperatingSystem'] = php_uname(); $error['OperatingSystem'] = php_uname();
$error['PHPVersion'] = phpversion(); $error['PHPVersion'] = phpversion();
@ -79,13 +82,13 @@ class ErrorHandler {
$json = json_encode($error); $json = json_encode($error);
$info = base64_encode($json); $info = base64_encode($json);
$url = ManiaControl::URL_WEBSERVICE . "errorreport?error=" . urlencode($info); $url = ManiaControl::URL_WEBSERVICE . 'errorreport?error=' . urlencode($info);
$success = FileUtil::loadFile($url); $success = FileUtil::loadFile($url);
if (!json_decode($success)) { if (!json_decode($success)) {
logMessage("Exception-Report failed!"); logMessage('Exception-Report failed!');
} else { } else {
logMessage("Exception successfully reported!"); logMessage('Exception successfully reported!');
} }
} }
@ -120,7 +123,7 @@ class ErrorHandler {
} }
/** /**
* Error Handler * ManiaControl Error Handler
* *
* @param int $errorNumber * @param int $errorNumber
* @param string $errorString * @param string $errorString
@ -129,22 +132,21 @@ class ErrorHandler {
* @return bool * @return bool
*/ */
public function handleError($errorNumber, $errorString, $errorFile = null, $errorLine = -1) { public function handleError($errorNumber, $errorString, $errorFile = null, $errorLine = -1) {
$userError = $this->isUserErrorNumber($errorNumber);
// Log error
$errorTag = $this->getErrorTag($errorNumber); $errorTag = $this->getErrorTag($errorNumber);
$message = $errorTag . ': ' . $errorString; $message = $errorTag . ': ' . $errorString;
$fileLine = $errorFile . ': ' . $errorLine; $fileLine = $errorFile . ': ' . $errorLine;
$traceMessage = $this->parseBackTrace(debug_backtrace()); $traceString = $this->parseBackTrace(debug_backtrace());
$logMessage = $message . PHP_EOL . $fileLine . PHP_EOL . $traceMessage;
$logMessage = $message . PHP_EOL . 'File&Line: ' . $fileLine . PHP_EOL . 'Trace: ' . $traceString;
logMessage($logMessage); logMessage($logMessage);
if ($this->reportErrors && !$userError) { if ($this->reportErrors && !$this->isUserErrorNumber($errorNumber)) {
$error = array(); $error = array();
$error["Type"] = "Error"; $error['Type'] = 'Error';
$error["Message"] = $message; $error['Message'] = $message;
$error["FileLine"] = $fileLine; $error['FileLine'] = $fileLine;
$error["Backtrace"] = $traceMessage; $error['Backtrace'] = $traceString;
$error['OperatingSystem'] = php_uname(); $error['OperatingSystem'] = php_uname();
$error['PHPVersion'] = phpversion(); $error['PHPVersion'] = phpversion();
@ -165,13 +167,13 @@ class ErrorHandler {
$json = json_encode($error); $json = json_encode($error);
$info = base64_encode($json); $info = base64_encode($json);
$url = ManiaControl::URL_WEBSERVICE . "errorreport?error=" . urlencode($info); $url = ManiaControl::URL_WEBSERVICE . 'errorreport?error=' . urlencode($info);
$success = FileUtil::loadFile($url); $success = FileUtil::loadFile($url);
if (!json_decode($success)) { if (!json_decode($success)) {
logMessage("Error-Report failed!"); logMessage('Error-Report failed!');
} else { } else {
logMessage("Error successfully reported!"); logMessage('Error successfully reported!');
} }
} }
if ($this->shouldStopExecution($errorNumber)) { if ($this->shouldStopExecution($errorNumber)) {
@ -181,16 +183,6 @@ class ErrorHandler {
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);
}
/** /**
* Get the Prefix for the given Error Level * Get the Prefix for the given Error Level
* *
@ -225,10 +217,11 @@ class ErrorHandler {
/** /**
* Parse the Debug Backtrace into a String for the Error Report * Parse the Debug Backtrace into a String for the Error Report
* return string *
* @return string
*/ */
private function parseBackTrace(array $backtrace) { private function parseBackTrace(array $backtrace) {
$traceString = 'Trace:'; $traceString = '';
$stepCount = 0; $stepCount = 0;
foreach ($backtrace as $traceStep) { foreach ($backtrace as $traceStep) {
$traceString .= PHP_EOL . '#' . $stepCount . ': '; $traceString .= PHP_EOL . '#' . $stepCount . ': ';
@ -254,6 +247,16 @@ class ErrorHandler {
return $traceString; return $traceString;
} }
/**
* 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);
}
/** /**
* Test if ManiaControl should stop its Execution * Test if ManiaControl should stop its Execution
* *