Centralized prefix for problem detail types

This commit is contained in:
Alejandro Celaya
2022-08-14 13:12:10 +02:00
parent 2650cb89b5
commit a41835573b
16 changed files with 77 additions and 51 deletions

View File

@@ -15,6 +15,9 @@ use Shlinkio\Shlink\Core\Exception\TagConflictException;
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use function explode;
use function Functional\last;
/** @deprecated */
class BackwardsCompatibleProblemDetailsException extends RuntimeException implements ProblemDetailsExceptionInterface
{
@@ -74,19 +77,20 @@ class BackwardsCompatibleProblemDetailsException extends RuntimeException implem
private function remapType(string $wrappedType): string
{
return match ($wrappedType) {
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',
$lastSegment = last(explode('/', $wrappedType));
return match ($lastSegment) {
ValidationException::ERROR_CODE => 'INVALID_ARGUMENT',
DeleteShortUrlException::ERROR_CODE => 'INVALID_SHORT_URL_DELETION',
DomainNotFoundException::ERROR_CODE => 'DOMAIN_NOT_FOUND',
ForbiddenTagOperationException::ERROR_CODE => 'FORBIDDEN_OPERATION',
InvalidUrlException::ERROR_CODE => 'INVALID_URL',
NonUniqueSlugException::ERROR_CODE => 'INVALID_SLUG',
ShortUrlNotFoundException::ERROR_CODE => 'INVALID_SHORTCODE',
TagConflictException::ERROR_CODE => 'TAG_CONFLICT',
TagNotFoundException::ERROR_CODE => 'TAG_NOT_FOUND',
MercureException::ERROR_CODE => 'MERCURE_NOT_CONFIGURED',
MissingAuthenticationException::ERROR_CODE => 'INVALID_AUTHORIZATION',
VerifyAuthenticationException::ERROR_CODE => 'INVALID_API_KEY',
default => $wrappedType,
};
}

View File

@@ -8,12 +8,14 @@ use Fig\Http\Message\StatusCodeInterface;
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use function Shlinkio\Shlink\Core\toProblemDetailsType;
class MercureException extends RuntimeException implements ProblemDetailsExceptionInterface
{
use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Mercure integration not configured';
public const TYPE = 'https://shlink.io/api/error/mercure-not-configured';
public const ERROR_CODE = 'mercure-not-configured';
public static function mercureNotConfigured(): self
{
@@ -21,7 +23,7 @@ class MercureException extends RuntimeException implements ProblemDetailsExcepti
$e->detail = $e->getMessage();
$e->title = self::TITLE;
$e->type = self::TYPE;
$e->type = toProblemDetailsType(self::ERROR_CODE);
$e->status = StatusCodeInterface::STATUS_NOT_IMPLEMENTED;
return $e;

View File

@@ -9,6 +9,7 @@ use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use function implode;
use function Shlinkio\Shlink\Core\toProblemDetailsType;
use function sprintf;
class MissingAuthenticationException extends RuntimeException implements ProblemDetailsExceptionInterface
@@ -16,7 +17,7 @@ class MissingAuthenticationException extends RuntimeException implements Problem
use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Invalid authorization';
public const TYPE = 'https://shlink.io/api/error/missing-authentication';
public const ERROR_CODE = 'missing-authentication';
public static function forHeaders(array $expectedHeaders): self
{
@@ -43,7 +44,7 @@ class MissingAuthenticationException extends RuntimeException implements Problem
$e->detail = $message;
$e->title = self::TITLE;
$e->type = self::TYPE;
$e->type = toProblemDetailsType(self::ERROR_CODE);
$e->status = StatusCodeInterface::STATUS_UNAUTHORIZED;
return $e;

View File

@@ -8,11 +8,13 @@ use Fig\Http\Message\StatusCodeInterface;
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use function Shlinkio\Shlink\Core\toProblemDetailsType;
class VerifyAuthenticationException extends RuntimeException implements ProblemDetailsExceptionInterface
{
use CommonProblemDetailsExceptionTrait;
public const TYPE = 'https://shlink.io/api/error/invalid-api-key';
public const ERROR_CODE = 'invalid-api-key';
public static function forInvalidApiKey(): self
{
@@ -20,7 +22,7 @@ class VerifyAuthenticationException extends RuntimeException implements ProblemD
$e->detail = $e->getMessage();
$e->title = 'Invalid API key';
$e->type = self::TYPE;
$e->type = toProblemDetailsType(self::ERROR_CODE);
$e->status = StatusCodeInterface::STATUS_UNAUTHORIZED;
return $e;