Created middleware to keep backwards compatibility on errors when using v1 and 2 of the API

This commit is contained in:
Alejandro Celaya
2022-08-13 16:50:19 +02:00
parent ed7be6eb99
commit cd4fe4362b
6 changed files with 183 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware\ErrorHandler;
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Exception\BackwardsCompatibleProblemDetailsException;
use function version_compare;
/** @deprecated */
class BackwardsCompatibleProblemDetailsHandler implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
return $handler->handle($request);
} catch (ProblemDetailsExceptionInterface $e) {
$version = $request->getAttribute('version') ?? '2';
throw version_compare($version, '3', '>=')
? $e
: BackwardsCompatibleProblemDetailsException::fromProblemDetails($e);
}
}
}