Disable plugins that have errors (#14383)

* Disable plugins that have errors
Disable plugin if a hook throws an error and set a notification
Move notification code to class, so we can access it
Clear notification when plugin is attempted to be enabled again

* fix style and lint fixes

* another lint fix and handle if property is missing
This commit is contained in:
Tony Murray
2022-09-25 22:47:58 -05:00
committed by GitHub
parent 333ba7c2cd
commit e990dfcb35
9 changed files with 222 additions and 197 deletions

View File

@@ -30,6 +30,7 @@ use App\Models\Plugin;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Support\Collection;
use LibreNMS\Util\Notifications;
use Log;
class PluginManager
@@ -107,16 +108,22 @@ class PluginManager
*/
public function call(string $hookType, array $args = [], ?string $plugin = null): Collection
{
try {
return $this->hooksFor($hookType, $args, $plugin)
->map(function ($hook) use ($args) {
return $this->hooksFor($hookType, $args, $plugin)
->map(function ($hook) use ($args, $hookType) {
try {
return app()->call([$hook['instance'], 'handle'], $this->fillArgs($args, $hook['plugin_name']));
});
} catch (Exception $e) {
Log::error("Error calling hook $hookType: " . $e->getMessage());
} catch (Exception|\Error $e) {
$name = $hook['plugin_name'];
Log::error("Error calling hook $hookType for $name: " . $e->getMessage());
return new Collection;
}
Notifications::create("Plugin $name disabled", "$name caused an error and was disabled, please check with the plugin creator to fix the error. The error can be found in logs/librenms.log", 'plugins', 2);
Plugin::where('plugin_name', $name)->update(['plugin_active' => 0]);
return 'HOOK FAILED';
}
})->filter(function ($hook) {
return $hook === 'HOOK FAILED';
});
}
/**