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.
This commit is contained in:
Franz Liedke 2019-08-10 11:03:44 +02:00
parent 9f71e2c3cb
commit d06493c61e
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
6 changed files with 12 additions and 11 deletions

View File

@ -58,7 +58,7 @@ class AdminServiceProvider extends AbstractServiceProvider
$pipe->pipe(new HttpMiddleware\HandleErrors( $pipe->pipe(new HttpMiddleware\HandleErrors(
$app->make(Registry::class), $app->make(Registry::class),
$app->inDebugMode() ? $app->make(WhoopsRenderer::class) : $app->make(ViewRenderer::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)); $pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class));

View File

@ -53,7 +53,7 @@ class ApiServiceProvider extends AbstractServiceProvider
$pipe->pipe(new HttpMiddleware\HandleErrors( $pipe->pipe(new HttpMiddleware\HandleErrors(
$app->make(Registry::class), $app->make(Registry::class),
$app->make(JsonApiRenderer::class), $app->make(JsonApiRenderer::class),
$app->make(Reporter::class) $app->tagged(Reporter::class)
)); ));
$pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class)); $pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class));

View File

@ -68,7 +68,7 @@ class ForumServiceProvider extends AbstractServiceProvider
$pipe->pipe(new HttpMiddleware\HandleErrors( $pipe->pipe(new HttpMiddleware\HandleErrors(
$app->make(Registry::class), $app->make(Registry::class),
$app->inDebugMode() ? $app->make(WhoopsRenderer::class) : $app->make(ViewRenderer::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)); $pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class));

View File

@ -71,6 +71,6 @@ class ErrorServiceProvider extends AbstractServiceProvider
); );
}); });
$this->app->singleton(Reporter::class, LogReporter::class); $this->app->tag(LogReporter::class, Reporter::class);
} }
} }

View File

@ -13,7 +13,6 @@ namespace Flarum\Http\Middleware;
use Flarum\Foundation\ErrorHandling\Formatter; use Flarum\Foundation\ErrorHandling\Formatter;
use Flarum\Foundation\ErrorHandling\Registry; use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\MiddlewareInterface as Middleware;
@ -33,15 +32,15 @@ class HandleErrors implements Middleware
protected $formatter; 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->registry = $registry;
$this->formatter = $formatter; $this->formatter = $formatter;
$this->reporter = $reporter; $this->reporters = $reporters;
} }
/** /**
@ -55,7 +54,9 @@ class HandleErrors implements Middleware
$error = $this->registry->handle($e); $error = $this->registry->handle($e);
if ($error->shouldBeReported()) { if ($error->shouldBeReported()) {
$this->reporter->report($error); foreach ($this->reporters as $reporter) {
$reporter->report($error);
}
} }
return $this->formatter->format($error, $request); return $this->formatter->format($error, $request);

View File

@ -43,7 +43,7 @@ class Installer implements AppInterface
$pipe->pipe(new HandleErrors( $pipe->pipe(new HandleErrors(
$this->container->make(Registry::class), $this->container->make(Registry::class),
$this->container->make(WhoopsRenderer::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($this->container->make(StartSession::class));
$pipe->pipe( $pipe->pipe(