diff --git a/module/Common/src/Expressive/ContentBasedErrorHandler.php b/module/Common/src/Expressive/ContentBasedErrorHandler.php index f0d4bc04..60e1dfdb 100644 --- a/module/Common/src/Expressive/ContentBasedErrorHandler.php +++ b/module/Common/src/Expressive/ContentBasedErrorHandler.php @@ -1,7 +1,7 @@ withStatus(404), 'Not found'); } // Track visit to this shortcode diff --git a/module/Core/templates/core/error/error.html.twig b/module/Core/templates/core/error/error.html.twig index 7426a33a..9821ec0e 100644 --- a/module/Core/templates/core/error/error.html.twig +++ b/module/Core/templates/core/error/error.html.twig @@ -12,7 +12,9 @@ {% block content %}
{{ translate('We encountered a %s %s error.') | format(status, reason) }}
+ {% if status != 404 %} +{{ translate('We encountered a %s %s error.') | format(status, reason) }}
+ {% endif %} {% if status == 404 %}{{ translate('This short URL doesn\'t seem to be valid.') }}
{{ translate('Make sure you included all the characters, with no extra punctuation.') }}
diff --git a/module/Rest/config/middleware-pipeline.config.php b/module/Rest/config/middleware-pipeline.config.php index ba228bf1..78c20c38 100644 --- a/module/Rest/config/middleware-pipeline.config.php +++ b/module/Rest/config/middleware-pipeline.config.php @@ -12,13 +12,5 @@ return [ ], 'priority' => 5, ], - - 'rest-not-found' => [ - 'path' => '/rest', - 'middleware' => [ - Middleware\NotFoundMiddleware::class, - ], - 'priority' => -1, - ], ], ]; diff --git a/module/Rest/config/services.config.php b/module/Rest/config/services.config.php index 926368c3..551c4c96 100644 --- a/module/Rest/config/services.config.php +++ b/module/Rest/config/services.config.php @@ -19,7 +19,6 @@ return [ Middleware\CrossDomainMiddleware::class => InvokableFactory::class, Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class, - Middleware\NotFoundMiddleware::class => AnnotatedFactory::class, ], ], diff --git a/module/Rest/src/Expressive/JsonErrorHandler.php b/module/Rest/src/Expressive/JsonErrorHandler.php index 0d02c45b..9294ec24 100644 --- a/module/Rest/src/Expressive/JsonErrorHandler.php +++ b/module/Rest/src/Expressive/JsonErrorHandler.php @@ -1,10 +1,11 @@ getStatusCode(); - $responsePhrase = $status < 400 ? 'Internal Server Error' : $response->getReasonPhrase(); - $status = $status < 400 ? 500 : $status; + $hasRoute = $request->getAttribute(RouteResult::class) !== null; + $isNotFound = ! $hasRoute && ! isset($err); + if ($isNotFound) { + $responsePhrase = 'Not found'; + $status = 404; + } else { + $status = $response->getStatusCode(); + $responsePhrase = $status < 400 ? 'Internal Server Error' : $response->getReasonPhrase(); + $status = $status < 400 ? 500 : $status; + } return new JsonResponse([ 'error' => $this->responsePhraseToCode($responsePhrase), diff --git a/module/Rest/src/Middleware/NotFoundMiddleware.php b/module/Rest/src/Middleware/NotFoundMiddleware.php deleted file mode 100644 index 720f39ca..00000000 --- a/module/Rest/src/Middleware/NotFoundMiddleware.php +++ /dev/null @@ -1,62 +0,0 @@ -translator = $translator; - } - - /** - * Process an incoming request and/or response. - * - * Accepts a server-side request and a response instance, and does - * something with them. - * - * If the response is not complete and/or further processing would not - * interfere with the work done in the middleware, or if the middleware - * wants to delegate to another process, it can use the `$out` callable - * if present. - * - * If the middleware does not return a value, execution of the current - * request is considered complete, and the response instance provided will - * be considered the response to return. - * - * Alternately, the middleware may return a response instance. - * - * Often, middleware will `return $out();`, with the assumption that a - * later middleware will return a response. - * - * @param Request $request - * @param Response $response - * @param null|callable $out - * @return null|Response - */ - public function __invoke(Request $request, Response $response, callable $out = null) - { - return new JsonResponse([ - 'error' => RestUtils::NOT_FOUND_ERROR, - 'message' => $this->translator->translate('Requested route does not exist.'), - ], 404); - } -}