Converted DeleteShortUrlException into a problem details exception

This commit is contained in:
Alejandro Celaya
2019-11-24 23:56:02 +01:00
parent 32b3c72bdf
commit 310032e303
5 changed files with 24 additions and 58 deletions

View File

@@ -4,12 +4,20 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Exception;
use Fig\Http\Message\StatusCodeInterface;
use Throwable;
use Zend\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use function sprintf;
class DeleteShortUrlException extends RuntimeException
class DeleteShortUrlException extends DomainException implements ProblemDetailsExceptionInterface
{
use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Cannot delete short URL';
public const TYPE = 'INVALID_SHORTCODE_DELETION'; // FIXME Should be INVALID_SHORT_URL_DELETION
/** @var int */
private $visitsThreshold;
@@ -21,11 +29,19 @@ class DeleteShortUrlException extends RuntimeException
public static function fromVisitsThreshold(int $threshold, string $shortCode): self
{
return new self($threshold, sprintf(
$e = new self($threshold, sprintf(
'Impossible to delete short URL with short code "%s" since it has more than "%s" visits.',
$shortCode,
$threshold
));
$e->detail = $e->getMessage();
$e->title = self::TITLE;
$e->type = self::TYPE;
$e->status = StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY;
$e->additional = ['threshold' => $threshold];
return $e;
}
public function getVisitsThreshold(): int