Added logic to properly map all existing errors from v3 to v2 in the API

This commit is contained in:
Alejandro Celaya 2022-08-13 17:15:04 +02:00
parent cd4fe4362b
commit 905f51fbd0
24 changed files with 51 additions and 30 deletions

View File

@ -69,7 +69,7 @@
"phpunit/phpunit": "^9.5", "phpunit/phpunit": "^9.5",
"roave/security-advisories": "dev-master", "roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.3.0", "shlinkio/php-coding-standard": "~2.3.0",
"shlinkio/shlink-test-utils": "^3.1.0", "shlinkio/shlink-test-utils": "^3.2",
"symfony/var-dumper": "^6.1", "symfony/var-dumper": "^6.1",
"veewee/composer-run-parallel": "^1.1" "veewee/composer-run-parallel": "^1.1"
}, },

View File

@ -16,7 +16,7 @@ class DeleteShortUrlException extends DomainException implements ProblemDetailsE
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Cannot delete short URL'; private const TITLE = 'Cannot delete short URL';
private const TYPE = 'INVALID_SHORT_URL_DELETION'; public const TYPE = 'https://shlink.io/api/error/invalid-short-url-deletion';
public static function fromVisitsThreshold(int $threshold, ShortUrlIdentifier $identifier): self public static function fromVisitsThreshold(int $threshold, ShortUrlIdentifier $identifier): self
{ {

View File

@ -15,7 +15,7 @@ class DomainNotFoundException extends DomainException implements ProblemDetailsE
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Domain not found'; private const TITLE = 'Domain not found';
private const TYPE = 'DOMAIN_NOT_FOUND'; public const TYPE = 'https://shlink.io/api/error/domain-not-found';
private function __construct(string $message, array $additional) private function __construct(string $message, array $additional)
{ {

View File

@ -13,7 +13,7 @@ class ForbiddenTagOperationException extends DomainException implements ProblemD
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Forbidden tag operation'; private const TITLE = 'Forbidden tag operation';
private const TYPE = 'FORBIDDEN_OPERATION'; public const TYPE = 'https://shlink.io/api/error/forbidden-tag-operation';
public static function forDeletion(): self public static function forDeletion(): self
{ {

View File

@ -16,7 +16,7 @@ class InvalidUrlException extends DomainException implements ProblemDetailsExcep
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Invalid URL'; private const TITLE = 'Invalid URL';
private const TYPE = 'INVALID_URL'; public const TYPE = 'https://shlink.io/api/error/invalid-url';
public static function fromUrl(string $url, ?Throwable $previous = null): self public static function fromUrl(string $url, ?Throwable $previous = null): self
{ {

View File

@ -16,7 +16,7 @@ class NonUniqueSlugException extends InvalidArgumentException implements Problem
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Invalid custom slug'; private const TITLE = 'Invalid custom slug';
private const TYPE = 'INVALID_SLUG'; public const TYPE = 'https://shlink.io/api/error/non-unique-slug';
public static function fromSlug(string $slug, ?string $domain = null): self public static function fromSlug(string $slug, ?string $domain = null): self
{ {

View File

@ -16,7 +16,7 @@ class ShortUrlNotFoundException extends DomainException implements ProblemDetail
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Short URL not found'; private const TITLE = 'Short URL not found';
private const TYPE = 'INVALID_SHORTCODE'; public const TYPE = 'https://shlink.io/api/error/short-url-not-found';
public static function fromNotFound(ShortUrlIdentifier $identifier): self public static function fromNotFound(ShortUrlIdentifier $identifier): self
{ {

View File

@ -16,7 +16,7 @@ class TagConflictException extends RuntimeException implements ProblemDetailsExc
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Tag conflict'; private const TITLE = 'Tag conflict';
private const TYPE = 'TAG_CONFLICT'; public const TYPE = 'https://shlink.io/api/error/tag-conflict';
public static function forExistingTag(TagRenaming $renaming): self public static function forExistingTag(TagRenaming $renaming): self
{ {

View File

@ -15,7 +15,7 @@ class TagNotFoundException extends DomainException implements ProblemDetailsExce
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Tag not found'; private const TITLE = 'Tag not found';
private const TYPE = 'TAG_NOT_FOUND'; public const TYPE = 'https://shlink.io/api/error/tag-not-found';
public static function fromTag(string $tag): self public static function fromTag(string $tag): self
{ {

View File

@ -37,7 +37,7 @@ class DeleteShortUrlExceptionTest extends TestCase
'threshold' => $threshold, 'threshold' => $threshold,
], $e->getAdditionalData()); ], $e->getAdditionalData());
self::assertEquals('Cannot delete short URL', $e->getTitle()); self::assertEquals('Cannot delete short URL', $e->getTitle());
self::assertEquals('INVALID_SHORT_URL_DELETION', $e->getType()); self::assertEquals('https://shlink.io/api/error/invalid-short-url-deletion', $e->getType());
self::assertEquals(422, $e->getStatus()); self::assertEquals(422, $e->getStatus());
} }

View File

@ -21,7 +21,7 @@ class DomainNotFoundExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Domain not found', $e->getTitle()); self::assertEquals('Domain not found', $e->getTitle());
self::assertEquals('DOMAIN_NOT_FOUND', $e->getType()); self::assertEquals('https://shlink.io/api/error/domain-not-found', $e->getType());
self::assertEquals(['id' => $id], $e->getAdditionalData()); self::assertEquals(['id' => $id], $e->getAdditionalData());
self::assertEquals(404, $e->getStatus()); self::assertEquals(404, $e->getStatus());
} }
@ -36,7 +36,7 @@ class DomainNotFoundExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Domain not found', $e->getTitle()); self::assertEquals('Domain not found', $e->getTitle());
self::assertEquals('DOMAIN_NOT_FOUND', $e->getType()); self::assertEquals('https://shlink.io/api/error/domain-not-found', $e->getType());
self::assertEquals(['authority' => $authority], $e->getAdditionalData()); self::assertEquals(['authority' => $authority], $e->getAdditionalData());
self::assertEquals(404, $e->getStatus()); self::assertEquals(404, $e->getStatus());
} }

View File

@ -25,7 +25,7 @@ class ForbiddenTagOperationExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Forbidden tag operation', $e->getTitle()); self::assertEquals('Forbidden tag operation', $e->getTitle());
self::assertEquals('FORBIDDEN_OPERATION', $e->getType()); self::assertEquals('https://shlink.io/api/error/forbidden-tag-operation', $e->getType());
self::assertEquals(403, $e->getStatus()); self::assertEquals(403, $e->getStatus());
} }

View File

@ -27,7 +27,7 @@ class InvalidUrlExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Invalid URL', $e->getTitle()); self::assertEquals('Invalid URL', $e->getTitle());
self::assertEquals('INVALID_URL', $e->getType()); self::assertEquals('https://shlink.io/api/error/invalid-url', $e->getType());
self::assertEquals(['url' => $url], $e->getAdditionalData()); self::assertEquals(['url' => $url], $e->getAdditionalData());
self::assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode()); self::assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode());
self::assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getStatus()); self::assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getStatus());

View File

@ -25,7 +25,7 @@ class NonUniqueSlugExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Invalid custom slug', $e->getTitle()); self::assertEquals('Invalid custom slug', $e->getTitle());
self::assertEquals('INVALID_SLUG', $e->getType()); self::assertEquals('https://shlink.io/api/error/non-unique-slug', $e->getType());
self::assertEquals(400, $e->getStatus()); self::assertEquals(400, $e->getStatus());
self::assertEquals($expectedAdditional, $e->getAdditionalData()); self::assertEquals($expectedAdditional, $e->getAdditionalData());
} }

View File

@ -29,7 +29,7 @@ class ShortUrlNotFoundExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Short URL not found', $e->getTitle()); self::assertEquals('Short URL not found', $e->getTitle());
self::assertEquals('INVALID_SHORTCODE', $e->getType()); self::assertEquals('https://shlink.io/api/error/short-url-not-found', $e->getType());
self::assertEquals(404, $e->getStatus()); self::assertEquals(404, $e->getStatus());
self::assertEquals($expectedAdditional, $e->getAdditionalData()); self::assertEquals($expectedAdditional, $e->getAdditionalData());
} }

View File

@ -23,7 +23,7 @@ class TagConflictExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Tag conflict', $e->getTitle()); self::assertEquals('Tag conflict', $e->getTitle());
self::assertEquals('TAG_CONFLICT', $e->getType()); self::assertEquals('https://shlink.io/api/error/tag-conflict', $e->getType());
self::assertEquals(['oldName' => $oldName, 'newName' => $newName], $e->getAdditionalData()); self::assertEquals(['oldName' => $oldName, 'newName' => $newName], $e->getAdditionalData());
self::assertEquals(409, $e->getStatus()); self::assertEquals(409, $e->getStatus());
} }

View File

@ -21,7 +21,7 @@ class TagNotFoundExceptionTest extends TestCase
self::assertEquals($expectedMessage, $e->getMessage()); self::assertEquals($expectedMessage, $e->getMessage());
self::assertEquals($expectedMessage, $e->getDetail()); self::assertEquals($expectedMessage, $e->getDetail());
self::assertEquals('Tag not found', $e->getTitle()); self::assertEquals('Tag not found', $e->getTitle());
self::assertEquals('TAG_NOT_FOUND', $e->getType()); self::assertEquals('https://shlink.io/api/error/tag-not-found', $e->getType());
self::assertEquals(['tag' => $tag], $e->getAdditionalData()); self::assertEquals(['tag' => $tag], $e->getAdditionalData());
self::assertEquals(404, $e->getStatus()); self::assertEquals(404, $e->getStatus());
} }

View File

@ -11,7 +11,7 @@ use function sprintf;
class ConfigProvider class ConfigProvider
{ {
private const ROUTES_PREFIX = '/rest/v{version:1|2}'; private const ROUTES_PREFIX = '/rest/v{version:1|2|3}';
private const UNVERSIONED_ROUTES_PREFIX = '/rest'; private const UNVERSIONED_ROUTES_PREFIX = '/rest';
public const UNVERSIONED_HEALTH_ENDPOINT_NAME = 'unversioned_health'; public const UNVERSIONED_HEALTH_ENDPOINT_NAME = 'unversioned_health';

View File

@ -5,6 +5,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Exception; namespace Shlinkio\Shlink\Rest\Exception;
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface; use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use Shlinkio\Shlink\Core\Exception\DeleteShortUrlException;
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
use Shlinkio\Shlink\Core\Exception\ForbiddenTagOperationException;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Exception\TagConflictException;
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Exception\ValidationException;
/** @deprecated */ /** @deprecated */
@ -68,6 +76,17 @@ class BackwardsCompatibleProblemDetailsException extends RuntimeException implem
{ {
return match ($wrappedType) { return match ($wrappedType) {
ValidationException::TYPE => 'INVALID_ARGUMENT', ValidationException::TYPE => 'INVALID_ARGUMENT',
DeleteShortUrlException::TYPE => 'INVALID_SHORT_URL_DELETION',
DomainNotFoundException::TYPE => 'DOMAIN_NOT_FOUND',
ForbiddenTagOperationException::TYPE => 'FORBIDDEN_OPERATION',
InvalidUrlException::TYPE => 'INVALID_URL',
NonUniqueSlugException::TYPE => 'INVALID_SLUG',
ShortUrlNotFoundException::TYPE => 'INVALID_SHORTCODE',
TagConflictException::TYPE => 'TAG_CONFLICT',
TagNotFoundException::TYPE => 'TAG_NOT_FOUND',
MercureException::TYPE => 'MERCURE_NOT_CONFIGURED',
MissingAuthenticationException::TYPE => 'INVALID_AUTHORIZATION',
VerifyAuthenticationException::TYPE => 'INVALID_API_KEY',
default => $wrappedType, default => $wrappedType,
}; };
} }

View File

@ -13,7 +13,7 @@ class MercureException extends RuntimeException implements ProblemDetailsExcepti
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Mercure integration not configured'; private const TITLE = 'Mercure integration not configured';
private const TYPE = 'MERCURE_NOT_CONFIGURED'; public const TYPE = 'https://shlink.io/api/error/mercure-not-configured';
public static function mercureNotConfigured(): self public static function mercureNotConfigured(): self
{ {

View File

@ -16,7 +16,7 @@ class MissingAuthenticationException extends RuntimeException implements Problem
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Invalid authorization'; private const TITLE = 'Invalid authorization';
private const TYPE = 'INVALID_AUTHORIZATION'; public const TYPE = 'https://shlink.io/api/error/missing-authentication';
public static function forHeaders(array $expectedHeaders): self public static function forHeaders(array $expectedHeaders): self
{ {

View File

@ -12,13 +12,15 @@ class VerifyAuthenticationException extends RuntimeException implements ProblemD
{ {
use CommonProblemDetailsExceptionTrait; use CommonProblemDetailsExceptionTrait;
public const TYPE = 'https://shlink.io/api/error/invalid-api-key';
public static function forInvalidApiKey(): self public static function forInvalidApiKey(): self
{ {
$e = new self('Provided API key does not exist or is invalid.'); $e = new self('Provided API key does not exist or is invalid.');
$e->detail = $e->getMessage(); $e->detail = $e->getMessage();
$e->title = 'Invalid API key'; $e->title = 'Invalid API key';
$e->type = 'INVALID_API_KEY'; $e->type = self::TYPE;
$e->status = StatusCodeInterface::STATUS_UNAUTHORIZED; $e->status = StatusCodeInterface::STATUS_UNAUTHORIZED;
return $e; return $e;

View File

@ -48,10 +48,10 @@ class ConfigProviderTest extends TestCase
['path' => '/health'], ['path' => '/health'],
], ],
[ [
['path' => '/rest/v{version:1|2}/foo'], ['path' => '/rest/v{version:1|2|3}/foo'],
['path' => '/rest/v{version:1|2}/bar'], ['path' => '/rest/v{version:1|2|3}/bar'],
['path' => '/rest/v{version:1|2}/baz/foo'], ['path' => '/rest/v{version:1|2|3}/baz/foo'],
['path' => '/rest/v{version:1|2}/health'], ['path' => '/rest/v{version:1|2|3}/health'],
['path' => '/rest/health', 'name' => ConfigProvider::UNVERSIONED_HEALTH_ENDPOINT_NAME], ['path' => '/rest/health', 'name' => ConfigProvider::UNVERSIONED_HEALTH_ENDPOINT_NAME],
], ],
]; ];
@ -62,9 +62,9 @@ class ConfigProviderTest extends TestCase
['path' => '/baz/foo'], ['path' => '/baz/foo'],
], ],
[ [
['path' => '/rest/v{version:1|2}/foo'], ['path' => '/rest/v{version:1|2|3}/foo'],
['path' => '/rest/v{version:1|2}/bar'], ['path' => '/rest/v{version:1|2|3}/bar'],
['path' => '/rest/v{version:1|2}/baz/foo'], ['path' => '/rest/v{version:1|2|3}/baz/foo'],
], ],
]; ];
} }

View File

@ -65,7 +65,7 @@ class MissingAuthenticationExceptionTest extends TestCase
private function assertCommonExceptionShape(MissingAuthenticationException $e): void private function assertCommonExceptionShape(MissingAuthenticationException $e): void
{ {
self::assertEquals('Invalid authorization', $e->getTitle()); self::assertEquals('Invalid authorization', $e->getTitle());
self::assertEquals('INVALID_AUTHORIZATION', $e->getType()); self::assertEquals('https://shlink.io/api/error/missing-authentication', $e->getType());
self::assertEquals(401, $e->getStatus()); self::assertEquals(401, $e->getStatus());
} }
} }