mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 01:03:13 +08:00
Updated InvalidShortCodeException to implement ProblemDetails
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user