From 194cf555dbbf884710e667e1c3e80418921188a5 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Tue, 28 Jul 2020 18:12:51 -0400 Subject: [PATCH] Add middleware in admin and forum to some errors via the frontend --- src/Admin/AdminServiceProvider.php | 1 + src/Forum/ForumServiceProvider.php | 1 + src/Http/HttpServiceProvider.php | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/Admin/AdminServiceProvider.php b/src/Admin/AdminServiceProvider.php index 24ba91b29..8a401fba3 100644 --- a/src/Admin/AdminServiceProvider.php +++ b/src/Admin/AdminServiceProvider.php @@ -58,6 +58,7 @@ class AdminServiceProvider extends AbstractServiceProvider HttpMiddleware\SetLocale::class, 'flarum.admin.route_resolver', HttpMiddleware\CheckCsrfToken::class, + 'flarum.http.frontend_handler', Middleware\RequireAdministrateAbility::class ]; }); diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index 0fc3ed579..75b4e1b53 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -69,6 +69,7 @@ class ForumServiceProvider extends AbstractServiceProvider 'flarum.forum.route_resolver', HttpMiddleware\CheckCsrfToken::class, HttpMiddleware\ShareErrorsFromSession::class, + 'flarum.http.frontend_handler', HttpMiddleware\FlarumPromotionHeader::class, ]; }); diff --git a/src/Http/HttpServiceProvider.php b/src/Http/HttpServiceProvider.php index ed6d6ee27..adb519fa0 100644 --- a/src/Http/HttpServiceProvider.php +++ b/src/Http/HttpServiceProvider.php @@ -16,6 +16,10 @@ use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\User; use Flarum\User\UsernameSlugDriver; use Illuminate\Support\Arr; +use Flarum\Foundation\ErrorHandling\Registry; +use Flarum\Foundation\ErrorHandling\Reporter; +use Flarum\Foundation\ErrorHandling\FrontendFormatter; +use Flarum\Foundation\ErrorHandling\WhoopsFormatter; class HttpServiceProvider extends AbstractServiceProvider { @@ -58,9 +62,28 @@ class HttpServiceProvider extends AbstractServiceProvider return $compiledDrivers; }); + $this->container->bind(SlugManager::class, function () { return new SlugManager($this->container->make('flarum.http.selectedSlugDrivers')); }); + + $this->container->singleton('flarum.http.frontend_exceptions', function () { + return [ + NotAuthenticatedException::class, // 401 + PermissionDeniedException::class, // 403 + ModelNotFoundException::class, // 404 + RouteNotFoundException::class, // 404 + ]; + }); + + $this->container->singleton('flarum.http.frontend_handler', function () { + return new Middleware\HandleErrors( + $this->container->make(Registry::class), + $this->container['flarum']->inDebugMode() ? $this->container->make(WhoopsFormatter::class) : $this->container->make(FrontendFormatter::class), + $this->container->tagged(Reporter::class), + $this->container->make('flarum.http.frontend_exceptions') + ); + }); } /**