Simplified code to render not-foubnd templates by infering the template to be used inside NotFoundHandler

This commit is contained in:
Alejandro Celaya
2019-11-02 18:49:24 +01:00
parent 01f60614ef
commit 906dfe60f8
6 changed files with 44 additions and 44 deletions

View File

@@ -11,7 +11,6 @@ use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shlinkio\Shlink\Common\Response\ResponseUtilsTrait;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
@@ -22,7 +21,6 @@ use Shlinkio\Shlink\PreviewGenerator\Service\PreviewGeneratorInterface;
class PreviewAction implements MiddlewareInterface
{
use ResponseUtilsTrait;
use ErrorResponseBuilderTrait;
/** @var PreviewGeneratorInterface */
private $previewGenerator;
@@ -60,7 +58,7 @@ class PreviewAction implements MiddlewareInterface
return $this->generateImageResponse($imagePath);
} catch (InvalidShortCodeException | EntityDoesNotExistException | PreviewGenerationException $e) {
$this->logger->warning('An error occurred while generating preview image. {e}', ['e' => $e]);
return $this->buildErrorResponse($request, $handler);
return $handler->handle($request);
}
}
}

View File

@@ -12,7 +12,6 @@ use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
@@ -21,8 +20,6 @@ use Zend\Expressive\Router\RouterInterface;
class QrCodeAction implements MiddlewareInterface
{
use ErrorResponseBuilderTrait;
private const DEFAULT_SIZE = 300;
private const MIN_SIZE = 50;
private const MAX_SIZE = 1000;
@@ -65,7 +62,7 @@ class QrCodeAction implements MiddlewareInterface
$this->urlShortener->shortCodeToUrl($shortCode, $domain);
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) {
$this->logger->warning('An error occurred while creating QR code. {e}', ['e' => $e]);
return $this->buildErrorResponse($request, $handler);
return $handler->handle($request);
}
$path = $this->router->generateUri(RedirectAction::class, ['shortCode' => $shortCode]);

View File

@@ -7,14 +7,11 @@ namespace Shlinkio\Shlink\Core\Action;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Options;
use Zend\Diactoros\Response\RedirectResponse;
class RedirectAction extends AbstractTrackingAction
{
use ErrorResponseBuilderTrait;
/** @var Options\NotFoundRedirectOptions */
private $redirectOptions;
@@ -27,6 +24,6 @@ class RedirectAction extends AbstractTrackingAction
protected function createErrorResp(ServerRequestInterface $request, RequestHandlerInterface $handler): Response
{
return $this->buildErrorResponse($request, $handler);
return $handler->handle($request);
}
}

View File

@@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action\Util;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Response\NotFoundHandler;
trait ErrorResponseBuilderTrait
{
private function buildErrorResponse(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$request = $request->withAttribute(NotFoundHandler::NOT_FOUND_TEMPLATE, 'ShlinkCore::invalid-short-code');
return $handler->handle($request);
}
}