mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Updated InvalidShortCodeException to implement ProblemDetails
This commit is contained in:
parent
850259290a
commit
09321eaa93
5 changed files with 23 additions and 45 deletions
|
@ -4,24 +4,31 @@ declare(strict_types=1);
|
|||
|
||||
namespace Shlinkio\Shlink\Core\Exception;
|
||||
|
||||
use Throwable;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Zend\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
|
||||
use Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
class InvalidShortCodeException extends RuntimeException
|
||||
class InvalidShortCodeException extends RuntimeException implements ProblemDetailsExceptionInterface
|
||||
{
|
||||
public static function fromCharset(string $shortCode, string $charSet, ?Throwable $previous = null): self
|
||||
{
|
||||
$code = $previous !== null ? $previous->getCode() : -1;
|
||||
return new static(
|
||||
sprintf('Provided short code "%s" does not match the char set "%s"', $shortCode, $charSet),
|
||||
$code,
|
||||
$previous
|
||||
);
|
||||
}
|
||||
use CommonProblemDetailsExceptionTrait;
|
||||
|
||||
public const TITLE = 'Invalid short code';
|
||||
public const TYPE = 'INVALID_SHORTCODE';
|
||||
|
||||
public static function fromNotFoundShortCode(string $shortCode): self
|
||||
{
|
||||
return new static(sprintf('Provided short code "%s" does not belong to a short URL', $shortCode));
|
||||
$e = new self(sprintf('No URL found for short code "%s"', $shortCode));
|
||||
$e->detail = $e->getMessage();
|
||||
$e->title = self::TITLE;
|
||||
$e->type = self::TYPE;
|
||||
$e->status = StatusCodeInterface::STATUS_NOT_FOUND;
|
||||
$e->additional = [
|
||||
'error' => $e->type,
|
||||
'message' => $e->detail,
|
||||
];
|
||||
|
||||
return $e;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,37 +4,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace ShlinkioTest\Shlink\Core\Exception;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||
use Throwable;
|
||||
|
||||
class InvalidShortCodeExceptionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider providePrevious
|
||||
*/
|
||||
public function properlyCreatesExceptionFromCharset(?Throwable $prev): void
|
||||
{
|
||||
$e = InvalidShortCodeException::fromCharset('abc123', 'def456', $prev);
|
||||
|
||||
$this->assertEquals('Provided short code "abc123" does not match the char set "def456"', $e->getMessage());
|
||||
$this->assertEquals($prev !== null ? $prev->getCode() : -1, $e->getCode());
|
||||
$this->assertEquals($prev, $e->getPrevious());
|
||||
}
|
||||
|
||||
public function providePrevious(): iterable
|
||||
{
|
||||
yield 'null previous' => [null];
|
||||
yield 'instance previous' => [new Exception('Previous error', 10)];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function properlyCreatesExceptionFromNotFoundShortCode(): void
|
||||
{
|
||||
$e = InvalidShortCodeException::fromNotFoundShortCode('abc123');
|
||||
|
||||
$this->assertEquals('Provided short code "abc123" does not belong to a short URL', $e->getMessage());
|
||||
$this->assertEquals('No URL found for short code "abc123"', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,15 +40,6 @@ class DeleteShortUrlAction extends AbstractRestAction
|
|||
try {
|
||||
$this->deleteShortUrlService->deleteByShortCode($shortCode);
|
||||
return new EmptyResponse();
|
||||
} catch (Exception\InvalidShortCodeException $e) {
|
||||
$this->logger->warning(
|
||||
'Provided short code {shortCode} does not belong to any URL. {e}',
|
||||
['e' => $e, 'shortCode' => $shortCode]
|
||||
);
|
||||
return new JsonResponse([
|
||||
'error' => RestUtils::getRestErrorCodeFromException($e),
|
||||
'message' => sprintf('No URL found for short code "%s"', $shortCode),
|
||||
], self::STATUS_NOT_FOUND);
|
||||
} catch (Exception\DeleteShortUrlException $e) {
|
||||
$this->logger->warning('Provided data is invalid. {e}', ['e' => $e]);
|
||||
$messagePlaceholder =
|
||||
|
|
|
@ -6,12 +6,14 @@ namespace Shlinkio\Shlink\Rest\Util;
|
|||
|
||||
use Shlinkio\Shlink\Common\Exception as Common;
|
||||
use Shlinkio\Shlink\Core\Exception as Core;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||
use Shlinkio\Shlink\Rest\Exception as Rest;
|
||||
use Throwable;
|
||||
|
||||
class RestUtils
|
||||
{
|
||||
public const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE';
|
||||
/** @deprecated */
|
||||
public const INVALID_SHORTCODE_ERROR = InvalidShortCodeException::TYPE;
|
||||
// FIXME Should be INVALID_SHORT_URL_DELETION
|
||||
public const INVALID_SHORTCODE_DELETION_ERROR = 'INVALID_SHORTCODE_DELETION';
|
||||
public const INVALID_URL_ERROR = 'INVALID_URL';
|
||||
|
|
|
@ -59,7 +59,6 @@ class DeleteShortUrlActionTest extends TestCase
|
|||
|
||||
public function provideExceptions(): iterable
|
||||
{
|
||||
yield 'not found' => [new Exception\InvalidShortCodeException(), RestUtils::INVALID_SHORTCODE_ERROR, 404];
|
||||
yield 'bad request' => [
|
||||
new Exception\DeleteShortUrlException(5),
|
||||
RestUtils::INVALID_SHORTCODE_DELETION_ERROR,
|
||||
|
|
Loading…
Add table
Reference in a new issue