mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +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,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Exception;
|
||||
|
||||
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
|
||||
/** @deprecated */
|
||||
class BackwardsCompatibleProblemDetailsException extends RuntimeException implements ProblemDetailsExceptionInterface
|
||||
{
|
||||
private function __construct(private readonly ProblemDetailsExceptionInterface $e)
|
||||
{
|
||||
parent::__construct($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
public static function fromProblemDetails(ProblemDetailsExceptionInterface $e): self
|
||||
{
|
||||
return new self($e);
|
||||
}
|
||||
|
||||
public function getStatus(): int
|
||||
{
|
||||
return $this->e->getStatus();
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->remapType($this->e->getType());
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->e->getTitle();
|
||||
}
|
||||
|
||||
public function getDetail(): string
|
||||
{
|
||||
return $this->e->getDetail();
|
||||
}
|
||||
|
||||
public function getAdditionalData(): array
|
||||
{
|
||||
return $this->e->getAdditionalData();
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->remapTypeInArray($this->e->toArray());
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return $this->remapTypeInArray($this->e->jsonSerialize());
|
||||
}
|
||||
|
||||
private function remapTypeInArray(array $wrappedArray): array
|
||||
{
|
||||
if (! isset($wrappedArray['type'])) {
|
||||
return $wrappedArray;
|
||||
}
|
||||
|
||||
return [...$wrappedArray, 'type' => $this->remapType($wrappedArray['type'])];
|
||||
}
|
||||
|
||||
private function remapType(string $wrappedType): string
|
||||
{
|
||||
return match ($wrappedType) {
|
||||
ValidationException::TYPE => 'INVALID_ARGUMENT',
|
||||
default => $wrappedType,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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