Updated InvalidShortCodeException to implement ProblemDetails

This commit is contained in:
Alejandro Celaya
2019-11-23 13:41:07 +01:00
parent 850259290a
commit 09321eaa93
5 changed files with 23 additions and 45 deletions

View File

@@ -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;
}
}