mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Created middleware to keep backwards compatibility on errors when using v1 and 2 of the API
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user