From 731a038f2962ea338542b586642e28da78ce00fe Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sat, 10 Aug 2019 11:03:44 +0200 Subject: [PATCH] Support multiple error reporters The error handling middleware now expects an array of reporters. Extensions can register new reporters in the container like this: use Flarum\Foundation\ErrorHandling\Reporter; $container->tag(NewReporter::class, Reporter::class); Note that this is just an implementation detail and will be hidden behind an extender. --- src/Admin/AdminServiceProvider.php | 2 +- src/Api/ApiServiceProvider.php | 2 +- src/Forum/ForumServiceProvider.php | 2 +- src/Foundation/ErrorServiceProvider.php | 2 +- src/Http/Middleware/HandleErrors.php | 13 +++++++------ src/Install/Installer.php | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Admin/AdminServiceProvider.php b/src/Admin/AdminServiceProvider.php index 16c7857d0..0edb6ff40 100644 --- a/src/Admin/AdminServiceProvider.php +++ b/src/Admin/AdminServiceProvider.php @@ -56,7 +56,7 @@ class AdminServiceProvider extends AbstractServiceProvider $pipe->pipe(new HttpMiddleware\HandleErrors( $app->make(Registry::class), $app->inDebugMode() ? $app->make(WhoopsRenderer::class) : $app->make(ViewRenderer::class), - $app->make(Reporter::class) + $app->tagged(Reporter::class) )); $pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class)); diff --git a/src/Api/ApiServiceProvider.php b/src/Api/ApiServiceProvider.php index bd765596c..7d744c99b 100644 --- a/src/Api/ApiServiceProvider.php +++ b/src/Api/ApiServiceProvider.php @@ -51,7 +51,7 @@ class ApiServiceProvider extends AbstractServiceProvider $pipe->pipe(new HttpMiddleware\HandleErrors( $app->make(Registry::class), $app->make(JsonApiRenderer::class), - $app->make(Reporter::class) + $app->tagged(Reporter::class) )); $pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class)); diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 1c16b56ed..230baac12 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -66,7 +66,7 @@ class ForumServiceProvider extends AbstractServiceProvider $pipe->pipe(new HttpMiddleware\HandleErrors( $app->make(Registry::class), $app->inDebugMode() ? $app->make(WhoopsRenderer::class) : $app->make(ViewRenderer::class), - $app->make(Reporter::class) + $app->tagged(Reporter::class) )); $pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class)); diff --git a/src/Foundation/ErrorServiceProvider.php b/src/Foundation/ErrorServiceProvider.php index 2a1deba21..ec82370ee 100644 --- a/src/Foundation/ErrorServiceProvider.php +++ b/src/Foundation/ErrorServiceProvider.php @@ -71,6 +71,6 @@ class ErrorServiceProvider extends AbstractServiceProvider ); }); - $this->app->singleton(Reporter::class, LogReporter::class); + $this->app->tag(LogReporter::class, Reporter::class); } } diff --git a/src/Http/Middleware/HandleErrors.php b/src/Http/Middleware/HandleErrors.php index ff077f105..94d749f8b 100644 --- a/src/Http/Middleware/HandleErrors.php +++ b/src/Http/Middleware/HandleErrors.php @@ -13,7 +13,6 @@ namespace Flarum\Http\Middleware; use Flarum\Foundation\ErrorHandling\Formatter; use Flarum\Foundation\ErrorHandling\Registry; -use Flarum\Foundation\ErrorHandling\Reporter; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\MiddlewareInterface as Middleware; @@ -33,15 +32,15 @@ class HandleErrors implements Middleware protected $formatter; /** - * @var Reporter + * @var \Flarum\Foundation\ErrorHandling\Reporter[] */ - protected $reporter; + protected $reporters; - public function __construct(Registry $registry, Formatter $formatter, Reporter $reporter) + public function __construct(Registry $registry, Formatter $formatter, array $reporters) { $this->registry = $registry; $this->formatter = $formatter; - $this->reporter = $reporter; + $this->reporters = $reporters; } /** @@ -55,7 +54,9 @@ class HandleErrors implements Middleware $error = $this->registry->handle($e); if ($error->shouldBeReported()) { - $this->reporter->report($error); + foreach ($this->reporters as $reporter) { + $reporter->report($error); + } } return $this->formatter->format($error, $request); diff --git a/src/Install/Installer.php b/src/Install/Installer.php index 6ac50539e..d91ff5fd0 100644 --- a/src/Install/Installer.php +++ b/src/Install/Installer.php @@ -41,7 +41,7 @@ class Installer implements AppInterface $pipe->pipe(new HandleErrors( $this->container->make(Registry::class), $this->container->make(WhoopsRenderer::class), - $this->container->make(Reporter::class) + $this->container->tagged(Reporter::class) )); $pipe->pipe($this->container->make(StartSession::class)); $pipe->pipe(