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'; const BEGINMAP = 'Callbacks.BeginMap';
/** EndMap Callback: Map*/ /** EndMap Callback: Map*/
const ENDMAP = 'Callbacks.EndMap'; const ENDMAP = 'Callbacks.EndMap';
//OLD Callbacks //OLD Callbacks
/** BeginMatch Callback: MatchNumber /** BeginMatch Callback: MatchNumber
* *

View File

@ -37,7 +37,7 @@ class ErrorHandler {
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->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')); set_exception_handler(array(&$this, 'handleException'));
register_shutdown_function(array(&$this, 'handleShutdown')); register_shutdown_function(array(&$this, 'handleShutdown'));
} }
@ -78,7 +78,7 @@ class ErrorHandler {
if (!$this->handlingError) { if (!$this->handlingError) {
// Reset error handler for safety // Reset error handler for safety
$this->handlingError = true; $this->handlingError = true;
set_error_handler(array(&$this, 'handleError'), -1); set_error_handler(array(&$this, 'handleError'));
} }
// Build log message // Build log message
@ -86,6 +86,8 @@ class ErrorHandler {
$isUserError = self::isUserErrorNumber($errorNumber); $isUserError = self::isUserErrorNumber($errorNumber);
$isFatalError = self::isFatalError($errorNumber); $isFatalError = self::isFatalError($errorNumber);
$isPluginError = false;
$traceString = null; $traceString = null;
$sourceClass = null; $sourceClass = null;
$traceSourceClass = null; $traceSourceClass = null;
@ -117,16 +119,22 @@ class ErrorHandler {
if ($fileLine) { if ($fileLine) {
$report['FileLine'] = self::stripBaseDir($fileLine); $report['FileLine'] = self::stripBaseDir($fileLine);
} }
if ($sourceClass) { if ($sourceClass) {
$report['SourceClass'] = $sourceClass; $report['SourceClass'] = $sourceClass;
$pluginId = PluginManager::getPluginId($sourceClass); $pluginId = PluginManager::getPluginId($sourceClass);
if ($pluginId > 0) { if ($pluginId > 0) {
$report['PluginId'] = $pluginId; $report['PluginId'] = $pluginId;
if ($isFatalError) { if ($isFatalError) {
$this->maniaControl->getPluginManager()->deactivatePlugin($sourceClass); $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) { if ($traceString) {
$report['Backtrace'] = $traceString; $report['Backtrace'] = $traceString;
} }
@ -161,12 +169,17 @@ class ErrorHandler {
} }
if ($isFatalError) { 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 // Disable safety state
$this->handlingError = false; $this->handlingError = false;
return false; return false;
} }
@ -381,7 +394,20 @@ class ErrorHandler {
$filePath = str_replace('core' . DIRECTORY_SEPARATOR, 'ManiaControl\\', $filePath); $filePath = str_replace('core' . DIRECTORY_SEPARATOR, 'ManiaControl\\', $filePath);
$className = str_replace('.php', '', $filePath); $className = str_replace('.php', '', $filePath);
$className = str_replace(DIRECTORY_SEPARATOR, '\\', $className); $className = str_replace(DIRECTORY_SEPARATOR, '\\', $className);
if (!class_exists($className, false)) { 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 null;
} }
return $className; return $className;

View File

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