improved error handling for plugins (if an error occur now in a plugin it always gets deactivated and maniacontrol directly restarted

This commit is contained in:
kremsy 2017-04-01 12:23:10 +02:00
parent 5abc3c3a16
commit 3e138a9065
3 changed files with 34 additions and 4 deletions

View File

@ -104,7 +104,7 @@ interface Callbacks {
const BEGINMAP = 'Callbacks.BeginMap';
/** EndMap Callback: Map*/
const ENDMAP = 'Callbacks.EndMap';
//OLD Callbacks
/** BeginMatch Callback: MatchNumber
*

View File

@ -37,7 +37,7 @@ class ErrorHandler {
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
set_error_handler(array(&$this, 'handleError'), -1);
set_error_handler(array(&$this, 'handleError')); //TODO before was -1 verify why
set_exception_handler(array(&$this, 'handleException'));
register_shutdown_function(array(&$this, 'handleShutdown'));
}
@ -78,7 +78,7 @@ class ErrorHandler {
if (!$this->handlingError) {
// Reset error handler for safety
$this->handlingError = true;
set_error_handler(array(&$this, 'handleError'), -1);
set_error_handler(array(&$this, 'handleError'));
}
// Build log message
@ -86,6 +86,8 @@ class ErrorHandler {
$isUserError = self::isUserErrorNumber($errorNumber);
$isFatalError = self::isFatalError($errorNumber);
$isPluginError = false;
$traceString = null;
$sourceClass = null;
$traceSourceClass = null;
@ -117,16 +119,22 @@ class ErrorHandler {
if ($fileLine) {
$report['FileLine'] = self::stripBaseDir($fileLine);
}
if ($sourceClass) {
$report['SourceClass'] = $sourceClass;
$pluginId = PluginManager::getPluginId($sourceClass);
if ($pluginId > 0) {
$report['PluginId'] = $pluginId;
if ($isFatalError) {
$this->maniaControl->getPluginManager()->deactivatePlugin($sourceClass);
$this->maniaControl->getChat()->sendError("Plugin " . $sourceClass . " has an Error -> The Plugin will be deactivated and ManiaControl restarted");
Logger::logError("Plugin " . $sourceClass . " has an Error -> The Plugin will be deactivated and ManiaControl restarted");
$isPluginError = true;
}
}
}
if ($traceString) {
$report['Backtrace'] = $traceString;
}
@ -161,12 +169,17 @@ class ErrorHandler {
}
if ($isFatalError) {
$this->maniaControl->quit('Quitting ManiaControl after Fatal Error.');
if ($isPluginError) {
$this->maniaControl->restart();
} else {
$this->maniaControl->quit('Quitting ManiaControl after Fatal Error.');
}
}
// Disable safety state
$this->handlingError = false;
return false;
}
@ -381,7 +394,20 @@ class ErrorHandler {
$filePath = str_replace('core' . DIRECTORY_SEPARATOR, 'ManiaControl\\', $filePath);
$className = str_replace('.php', '', $filePath);
$className = str_replace(DIRECTORY_SEPARATOR, '\\', $className);
if (!class_exists($className, false)) {
//For Classes With different Folder Namespaces
$splitNameSpace = explode('\\', $className);
if (is_array($splitNameSpace)) {
$className = end($splitNameSpace);
}
foreach (get_declared_classes() as $declared_class) {
if (strpos($declared_class, $className) !== false) {
return $declared_class;
}
}
return null;
}
return $className;

View File

@ -133,7 +133,10 @@ class PluginManager {
if (!$pluginClass) {
return false;
}
if (!$this->isPluginActive($pluginClass)) {
//If Error Occured while loading Plugin
$this->savePluginStatus($pluginClass, false);
return false;
}
@ -388,6 +391,7 @@ class PluginManager {
if ($this->isPluginActive($pluginClass)) {
return false;
}
/** @var Plugin $plugin */
$plugin = new $pluginClass();