From bdac88b5733643b9c5dabae9e09a64d9f6e41d58 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 14 Aug 2019 19:43:36 +0200 Subject: [PATCH] Determine error view and message based on type ...not based on status code. To simplify this logic, we now use the same error "type" both when routes are not found and specific models are not found. One exception is ours, one is from Laravel, but for the purposes of error handling they should be treated the same. Fixes flarum/core#1641. --- src/Foundation/ErrorHandling/ViewRenderer.php | 17 ++++++++++------- src/Foundation/ErrorServiceProvider.php | 5 ++--- src/Http/Exception/RouteNotFoundException.php | 2 +- ....blade.php => csrf_token_mismatch.blade.php} | 2 +- .../{404.blade.php => not_found.blade.php} | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) rename views/error/{419.blade.php => csrf_token_mismatch.blade.php} (66%) rename views/error/{404.blade.php => not_found.blade.php} (57%) diff --git a/src/Foundation/ErrorHandling/ViewRenderer.php b/src/Foundation/ErrorHandling/ViewRenderer.php index 4ac50ffd7..438a2d9d6 100644 --- a/src/Foundation/ErrorHandling/ViewRenderer.php +++ b/src/Foundation/ErrorHandling/ViewRenderer.php @@ -51,26 +51,29 @@ class ViewRenderer implements Formatter return new HtmlResponse($view->render(), $error->getStatusCode()); } + const ERRORS_WITH_VIEWS = ['csrf_token_mismatch', 'not_found']; + private function determineView(HandledError $error): string { - $view = [ - 'route_not_found' => '404', - 'csrf_token_mismatch' => '419', - ][$error->getType()] ?? 'default'; + $type = $error->getType(); - return "flarum.forum::error.$view"; + if (in_array($type, self::ERRORS_WITH_VIEWS)) { + return "flarum.forum::error.$type"; + } else { + return 'flarum.forum::error.default'; + } } private function getMessage(HandledError $error) { - return $this->getTranslationIfExists($error->getStatusCode()) + return $this->getTranslationIfExists($error->getType()) ?? $this->getTranslationIfExists('unknown') ?? 'An error occurred while trying to load this page.'; } private function getTranslationIfExists(string $errorType) { - $key = "core.views.error.${errorType}_message"; + $key = "core.views.error.$errorType"; $translation = $this->translator->trans($key, ['{forum}' => $this->settings->get('forum_title')]); return $translation === $key ? null : $translation; diff --git a/src/Foundation/ErrorServiceProvider.php b/src/Foundation/ErrorServiceProvider.php index ec82370ee..aa7060bb5 100644 --- a/src/Foundation/ErrorServiceProvider.php +++ b/src/Foundation/ErrorServiceProvider.php @@ -38,8 +38,7 @@ class ErrorServiceProvider extends AbstractServiceProvider 'permission_denied' => 403, // 404 Not Found - 'model_not_found' => 404, - 'route_not_found' => 404, + 'not_found' => 404, // 405 Method Not Allowed 'method_not_allowed' => 405, @@ -52,7 +51,7 @@ class ErrorServiceProvider extends AbstractServiceProvider $this->app->singleton('flarum.error.classes', function () { return [ InvalidParameterException::class => 'invalid_parameter', - ModelNotFoundException::class => 'model_not_found', + ModelNotFoundException::class => 'not_found', ]; }); diff --git a/src/Http/Exception/RouteNotFoundException.php b/src/Http/Exception/RouteNotFoundException.php index 820da488e..183742585 100644 --- a/src/Http/Exception/RouteNotFoundException.php +++ b/src/Http/Exception/RouteNotFoundException.php @@ -18,6 +18,6 @@ class RouteNotFoundException extends Exception implements KnownError { public function getType(): string { - return 'route_not_found'; + return 'not_found'; } } diff --git a/views/error/419.blade.php b/views/error/csrf_token_mismatch.blade.php similarity index 66% rename from views/error/419.blade.php rename to views/error/csrf_token_mismatch.blade.php index 63c5fb4be..9673c6763 100644 --- a/views/error/419.blade.php +++ b/views/error/csrf_token_mismatch.blade.php @@ -6,7 +6,7 @@

- {{ $translator->trans('core.views.error.419_return_link') }} + {{ $translator->trans('core.views.error.csrf_token_mismatch_return_link') }}

@endsection diff --git a/views/error/404.blade.php b/views/error/not_found.blade.php similarity index 57% rename from views/error/404.blade.php rename to views/error/not_found.blade.php index 928b4a8a3..557bc3cb9 100644 --- a/views/error/404.blade.php +++ b/views/error/not_found.blade.php @@ -6,7 +6,7 @@

- {{ $translator->trans('core.views.error.404_return_link', ['{forum}' => $settings->get('forum_title')]) }} + {{ $translator->trans('core.views.error.not_found_return_link', ['{forum}' => $settings->get('forum_title')]) }}

@endsection