Skip to content

Commit

Permalink
Catch \Throwable when utilizing hooks
Browse files Browse the repository at this point in the history
Hooks shouldn't be able to fatally end
execution.
  • Loading branch information
nilmerg committed Jul 24, 2024
1 parent cdbaea0 commit 6ad38f6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Icinga\Module\Icingadb\Hook\ActionsHook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
Expand All @@ -20,6 +19,7 @@
use ipl\Html\Text;
use ipl\Orm\Model;
use ipl\Web\Widget\Link;
use Throwable;

use function ipl\Stdlib\get_php_type;

Expand Down Expand Up @@ -72,7 +72,7 @@ final public static function loadActions(Model $object): HtmlElement
'data-icinga-module' => $moduleName
]), HtmlString::create($renderedLink)));
}
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Failed to load object actions: %s\n%s", $e, $e->getTraceAsString());
$list->addHtml(new HtmlElement('li', null, Text::create(IcingaException::describe($e))));
}
Expand Down
6 changes: 3 additions & 3 deletions library/Icingadb/Hook/CustomVarRendererHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Icinga\Module\Icingadb\Hook;

use Closure;
use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
use ipl\Orm\Model;
use Throwable;

abstract class CustomVarRendererHook
{
Expand Down Expand Up @@ -68,7 +68,7 @@ final public static function prepareForObject(Model $object): Closure
if ($hook->prefetchForObject($object)) {
$hooks[] = $hook;
}
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error('Failed to load hook %s:', get_class($hook), $e);
}
}
Expand All @@ -84,7 +84,7 @@ final public static function prepareForObject(Model $object): Closure
$renderedKey = $hook->renderCustomVarKey($key);
$renderedValue = $hook->renderCustomVarValue($key, $value);
$group = $hook->identifyCustomVarGroup($key);
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error('Failed to use hook %s:', get_class($hook), $e);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Icinga\Module\Icingadb\Hook\ExtensionHook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
Expand All @@ -25,6 +24,7 @@
use ipl\Html\Text;
use ipl\Html\ValidHtml;
use ipl\Orm\Model;
use Throwable;

use function ipl\Stdlib\get_php_type;

Expand Down Expand Up @@ -110,7 +110,7 @@ final public static function loadExtensions(Model $object): array
]),
HtmlString::create($extension)
);
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Failed to load detail extension: %s\n%s", $e, $e->getTraceAsString());
$extensions[$location] = Text::create(IcingaException::describe($e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Icinga\Module\Icingadb\Hook\ExtensionHook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
Expand All @@ -19,6 +18,7 @@
use ipl\Orm\Query;
use ipl\Stdlib\BaseFilter;
use ipl\Stdlib\Filter;
use Throwable;

abstract class ObjectsDetailExtensionHook extends BaseExtensionHook
{
Expand Down Expand Up @@ -90,7 +90,7 @@ final public static function loadExtensions(string $objectType, Query $query, Fi
]),
HtmlString::create($extension)
);
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Failed to load details extension: %s\n%s", $e, $e->getTraceAsString());
$extensions[$location] = Text::create(IcingaException::describe($e));
}
Expand Down
4 changes: 2 additions & 2 deletions library/Icingadb/Hook/PluginOutputHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Icinga\Module\Icingadb\Hook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
use Throwable;

abstract class PluginOutputHook
{
Expand Down Expand Up @@ -53,7 +53,7 @@ final public static function processOutput(string $output, string $commandName,
if ($hook->isSupportedCommand($commandName)) {
$output = $hook->render($output, $commandName, $enrichOutput);
}
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Unable to process plugin output: %s\n%s", $e, $e->getTraceAsString());
}
}
Expand Down
4 changes: 2 additions & 2 deletions library/Icingadb/Hook/TabHook/HookActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Icinga\Module\Icingadb\Hook\TabHook;

use Exception;
use Generator;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Hook\TabHook;
use ipl\Html\ValidHtml;
use ipl\Orm\Model;
use ipl\Stdlib\Str;
use Throwable;

/**
* Trait HookActions
Expand Down Expand Up @@ -78,7 +78,7 @@ protected function loadTabHooks(): array
if ($hook->shouldBeShown($this->objectToLoadTabsFor)) {
$this->tabHooks[Str::camel($hook->getName())] = $hook;
}
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Failed to load tab hook: %s\n%s", $e, $e->getTraceAsString());
}
}
Expand Down
8 changes: 4 additions & 4 deletions library/Icingadb/Widget/Detail/ObjectDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Icinga\Module\Icingadb\Widget\Detail;

use Exception;
use Icinga\Application\ClassLoader;
use Icinga\Application\Hook;
use Icinga\Application\Hook\GrapherHook;
Expand Down Expand Up @@ -52,6 +51,7 @@
use ipl\Stdlib\Filter;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\StateBall;
use Throwable;

class ObjectDetail extends BaseHtmlElement
{
Expand Down Expand Up @@ -169,7 +169,7 @@ protected function createActions()
if (! isset($nativeExtensionProviders[$moduleName])) {
try {
$navigation->merge($hook->getNavigation($this->compatObject()));
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Failed to load legacy action hook: %s\n%s", $e, $e->getTraceAsString());
$navigation->addItem($moduleName, ['label' => IcingaException::describe($e), 'url' => '#']);
}
Expand Down Expand Up @@ -469,7 +469,7 @@ protected function createExtensions(): array

try {
$graph = HtmlString::create($grapher->getPreviewHtml($this->compatObject()));
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Failed to load legacy grapher: %s\n%s", $e, $e->getTraceAsString());
$graph = Text::create(IcingaException::describe($e));
}
Expand Down Expand Up @@ -505,7 +505,7 @@ protected function createExtensions(): array
]),
HtmlString::create($renderedExtension)
);
} catch (Exception $e) {
} catch (Throwable $e) {
Logger::error("Failed to load legacy detail extension: %s\n%s", $e, $e->getTraceAsString());
$extensionHtml = Text::create(IcingaException::describe($e));
}
Expand Down

0 comments on commit 6ad38f6

Please sign in to comment.