From 7e3dc0ba98bf019c2804e5c74fb6061b16fb712f Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 27 Aug 2020 12:04:36 +0200 Subject: Better handling of plugin incompatibility If a PHP is raised while executing plugin hook, Shaarli will display an error instead of rendering the error page (or just ending in fatal error for default hooks). Also added phpErrorHandler which is handled differently that regular errorHandler by Slim.: --- application/container/ContainerBuilder.php | 3 +++ application/container/ShaarliContainer.php | 4 ++-- application/front/controller/visitor/ShaarliVisitorController.php | 3 ++- application/plugin/PluginManager.php | 7 ++++++- 4 files changed, 13 insertions(+), 4 deletions(-) (limited to 'application') diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php index 4a1a6ea7..58067c99 100644 --- a/application/container/ContainerBuilder.php +++ b/application/container/ContainerBuilder.php @@ -152,6 +152,9 @@ class ContainerBuilder $container['errorHandler'] = function (ShaarliContainer $container): ErrorController { return new ErrorController($container); }; + $container['phpErrorHandler'] = function (ShaarliContainer $container): ErrorController { + return new ErrorController($container); + }; return $container; } diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php index c4fe753e..9a9a974a 100644 --- a/application/container/ShaarliContainer.php +++ b/application/container/ShaarliContainer.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shaarli\Container; -use http\Cookie; use Shaarli\Bookmark\BookmarkServiceInterface; use Shaarli\Config\ConfigManager; use Shaarli\Feed\FeedBuilder; @@ -30,7 +29,7 @@ use Slim\Container; * @property CookieManager $cookieManager * @property ConfigManager $conf * @property mixed[] $environment $_SERVER automatically injected by Slim - * @property callable $errorHandler Overrides default Slim error display + * @property callable $errorHandler Overrides default Slim exception display * @property FeedBuilder $feedBuilder * @property FormatterFactory $formatterFactory * @property History $history @@ -39,6 +38,7 @@ use Slim\Container; * @property NetscapeBookmarkUtils $netscapeBookmarkUtils * @property PageBuilder $pageBuilder * @property PageCacheManager $pageCacheManager + * @property callable $phpErrorHandler Overrides default Slim PHP error display * @property PluginManager $pluginManager * @property SessionManager $sessionManager * @property Thumbnailer $thumbnailer diff --git a/application/front/controller/visitor/ShaarliVisitorController.php b/application/front/controller/visitor/ShaarliVisitorController.php index 47057d97..f17c8ed3 100644 --- a/application/front/controller/visitor/ShaarliVisitorController.php +++ b/application/front/controller/visitor/ShaarliVisitorController.php @@ -58,10 +58,11 @@ abstract class ShaarliVisitorController { $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL)); $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE)); - $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); $this->executeDefaultHooks($template); + $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); + return $this->container->pageBuilder->render($template, $this->container->basePath); } diff --git a/application/plugin/PluginManager.php b/application/plugin/PluginManager.php index b3e8b2f8..2d93cb3a 100644 --- a/application/plugin/PluginManager.php +++ b/application/plugin/PluginManager.php @@ -116,7 +116,12 @@ class PluginManager $hookFunction = $this->buildHookName($hook, $plugin); if (function_exists($hookFunction)) { - $data = call_user_func($hookFunction, $data, $this->conf); + try { + $data = call_user_func($hookFunction, $data, $this->conf); + } catch (\Throwable $e) { + $error = $plugin . t(' [plugin incompatibility]: ') . $e->getMessage(); + $this->errors = array_unique(array_merge($this->errors, [$error])); + } } } } -- cgit v1.2.3