mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-27 16:26:37 +03:00
Added middleware that mitigates big error traces being logged for those not using mercure
This commit is contained in:
parent
5affe64b61
commit
6983f9b2bf
5 changed files with 47 additions and 11 deletions
|
@ -6,7 +6,9 @@ namespace Shlinkio\Shlink\Rest;
|
|||
|
||||
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
|
||||
use Laminas\ServiceManager\Factory\InvokableFactory;
|
||||
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory;
|
||||
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Common\Mercure\LcobucciJwtProvider;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainService;
|
||||
use Shlinkio\Shlink\Core\Options;
|
||||
|
@ -49,6 +51,7 @@ return [
|
|||
Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ConfigAbstractFactory::class,
|
||||
Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class => ConfigAbstractFactory::class,
|
||||
Middleware\ShortUrl\OverrideDomainMiddleware::class => ConfigAbstractFactory::class,
|
||||
Middleware\Mercure\NotConfiguredMercureErrorHandler::class => ConfigAbstractFactory::class,
|
||||
],
|
||||
],
|
||||
|
||||
|
@ -90,6 +93,10 @@ return [
|
|||
'config.url_shortener.default_short_codes_length',
|
||||
],
|
||||
Middleware\ShortUrl\OverrideDomainMiddleware::class => [DomainService::class],
|
||||
Middleware\Mercure\NotConfiguredMercureErrorHandler::class => [
|
||||
ProblemDetailsResponseFactory::class,
|
||||
LoggerInterface::class,
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
|
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Shlinkio\Shlink\Rest;
|
||||
|
||||
use Shlinkio\Shlink\Rest\Middleware\Mercure\NotConfiguredMercureErrorHandler;
|
||||
|
||||
$contentNegotiationMiddleware = Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class;
|
||||
$dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class;
|
||||
$overrideDomainMiddleware = Middleware\ShortUrl\OverrideDomainMiddleware::class;
|
||||
|
@ -46,7 +48,7 @@ return [
|
|||
Action\Domain\ListDomainsAction::getRouteDef(),
|
||||
Action\Domain\DomainRedirectsAction::getRouteDef(),
|
||||
|
||||
Action\MercureInfoAction::getRouteDef(),
|
||||
Action\MercureInfoAction::getRouteDef([NotConfiguredMercureErrorHandler::class]),
|
||||
],
|
||||
|
||||
];
|
||||
|
|
|
@ -10,7 +10,6 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
|
||||
use Shlinkio\Shlink\Rest\Exception\MercureException;
|
||||
use Throwable;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
|
@ -32,12 +31,7 @@ class MercureInfoAction extends AbstractRestAction
|
|||
|
||||
$days = $this->mercureConfig['jwt_days_duration'] ?? 1;
|
||||
$expiresAt = Chronos::now()->addDays($days);
|
||||
|
||||
try {
|
||||
$jwt = $this->jwtProvider->buildSubscriptionToken($expiresAt);
|
||||
} catch (Throwable $e) {
|
||||
throw MercureException::mercureNotConfigured($e);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'mercureHubUrl' => sprintf('%s/.well-known/mercure', $hubUrl),
|
||||
|
|
|
@ -7,7 +7,6 @@ namespace Shlinkio\Shlink\Rest\Exception;
|
|||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
|
||||
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
||||
use Throwable;
|
||||
|
||||
class MercureException extends RuntimeException implements ProblemDetailsExceptionInterface
|
||||
{
|
||||
|
@ -16,9 +15,9 @@ class MercureException extends RuntimeException implements ProblemDetailsExcepti
|
|||
private const TITLE = 'Mercure integration not configured';
|
||||
private const TYPE = 'MERCURE_NOT_CONFIGURED';
|
||||
|
||||
public static function mercureNotConfigured(?Throwable $prev = null): self
|
||||
public static function mercureNotConfigured(): self
|
||||
{
|
||||
$e = new self('This Shlink instance is not integrated with a mercure hub.', 1, $prev);
|
||||
$e = new self('This Shlink instance is not integrated with a mercure hub.');
|
||||
|
||||
$e->detail = $e->getMessage();
|
||||
$e->title = self::TITLE;
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Middleware\Mercure;
|
||||
|
||||
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Rest\Exception\MercureException;
|
||||
|
||||
class NotConfiguredMercureErrorHandler implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(private ProblemDetailsResponseFactory $respFactory, private LoggerInterface $logger)
|
||||
{
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
try {
|
||||
return $handler->handle($request);
|
||||
} catch (MercureException $e) {
|
||||
// Throwing this kind of exception makes a big error trace to be logged, for anyone who has decided to not
|
||||
// use mercure.
|
||||
// It happens every time the shlink-web-client is opened, so this mitigates the problem by just logging a
|
||||
// simple warning, and casting the exception to a response on the fly.
|
||||
$this->logger->warning($e->getMessage());
|
||||
return $this->respFactory->createResponseFromThrowable($request, $e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue