Removed duplicated error handling for 404 errors

This commit is contained in:
Alejandro Celaya 2016-07-28 09:40:36 +02:00
parent 36259588db
commit af9193f721
8 changed files with 18 additions and 79 deletions

View file

@ -1,7 +1,7 @@
<?php
namespace Shlinkio\Shlink\Common\Expressive;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Zend\ServiceManager\AbstractPluginManager;

View file

@ -1,7 +1,7 @@
<?php
namespace Shlinkio\Shlink\Common\Expressive;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
interface ErrorHandlerInterface

View file

@ -70,7 +70,7 @@ class RedirectAction implements MiddlewareInterface
// If provided shortCode does not belong to a valid long URL, dispatch next middleware, which will trigger
// a not-found error
if (! isset($longUrl)) {
return $out($request, $response);
return $out($request, $response->withStatus(404), 'Not found');
}
// Track visit to this shortcode

View file

@ -12,7 +12,9 @@
{% block content %}
<h1>{{ translate('Oops!') }}</h1>
<hr>
<p>{{ translate('We encountered a %s %s error.') | format(status, reason) }}</p>
{% if status != 404 %}
<p>{{ translate('We encountered a %s %s error.') | format(status, reason) }}</p>
{% endif %}
{% if status == 404 %}
<p>{{ translate('This short URL doesn\'t seem to be valid.') }}</p>
<p>{{ translate('Make sure you included all the characters, with no extra punctuation.') }}</p>

View file

@ -12,13 +12,5 @@ return [
],
'priority' => 5,
],
'rest-not-found' => [
'path' => '/rest',
'middleware' => [
Middleware\NotFoundMiddleware::class,
],
'priority' => -1,
],
],
];

View file

@ -19,7 +19,6 @@ return [
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class,
Middleware\NotFoundMiddleware::class => AnnotatedFactory::class,
],
],

View file

@ -1,10 +1,11 @@
<?php
namespace Shlinkio\Shlink\Rest\Expressive;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Shlinkio\Shlink\Common\Expressive\ErrorHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Router\RouteResult;
class JsonErrorHandler implements ErrorHandlerInterface
{
@ -18,9 +19,16 @@ class JsonErrorHandler implements ErrorHandlerInterface
*/
public function __invoke(Request $request, Response $response, $err = null)
{
$status = $response->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),

View file

@ -1,62 +0,0 @@
<?php
namespace Shlinkio\Shlink\Rest\Middleware;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface;
use Zend\Stratigility\MiddlewareInterface;
class NotFoundMiddleware implements MiddlewareInterface
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* NotFoundMiddleware constructor.
* @param TranslatorInterface $translator
*
* @Inject({"translator"})
*/
public function __construct(TranslatorInterface $translator)
{
$this->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);
}
}