Converted ValidationException into a problem details exception

This commit is contained in:
Alejandro Celaya
2019-11-24 23:45:40 +01:00
parent c1eee2246b
commit 32b3c72bdf
8 changed files with 47 additions and 78 deletions

View File

@@ -12,7 +12,6 @@ use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse;
abstract class AbstractCreateShortUrlAction extends AbstractRestAction
@@ -32,22 +31,9 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
$this->domainConfig = $domainConfig;
}
/**
* @param Request $request
* @return Response
*/
public function handle(Request $request): Response
{
try {
$shortUrlData = $this->buildShortUrlData($request);
} catch (ValidationException $e) {
$this->logger->warning('Provided data is invalid. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
'message' => $e->getMessage(),
], self::STATUS_BAD_REQUEST);
}
$shortUrlData = $this->buildShortUrlData($request);
$longUrl = $shortUrlData->getLongUrl();
$tags = $shortUrlData->getTags();
$shortUrlMeta = $shortUrlData->getMeta();

View File

@@ -7,13 +7,10 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Diactoros\Response\JsonResponse;
class EditShortUrlAction extends AbstractRestAction
{
@@ -29,32 +26,12 @@ class EditShortUrlAction extends AbstractRestAction
$this->shortUrlService = $shortUrlService;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
* @throws \InvalidArgumentException
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$postData = (array) $request->getParsedBody();
$shortCode = $request->getAttribute('shortCode', '');
try {
$this->shortUrlService->updateMetadataByShortCode(
$shortCode,
ShortUrlMeta::createFromRawData($postData)
);
return new EmptyResponse();
} catch (Exception\ValidationException $e) {
$this->logger->warning('Provided data is invalid. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => 'Provided data is invalid.',
], self::STATUS_BAD_REQUEST);
}
$this->shortUrlService->updateMetadataByShortCode($shortCode, ShortUrlMeta::createFromRawData($postData));
return new EmptyResponse();
}
}

View File

@@ -6,19 +6,19 @@ namespace Shlinkio\Shlink\Rest\Util;
use Shlinkio\Shlink\Common\Exception as Common;
use Shlinkio\Shlink\Core\Exception as Core;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Rest\Exception as Rest;
use Throwable;
class RestUtils
{
/** @deprecated */
public const INVALID_SHORTCODE_ERROR = ShortUrlNotFoundException::TYPE;
public const INVALID_SHORTCODE_ERROR = Core\ShortUrlNotFoundException::TYPE;
// FIXME Should be INVALID_SHORT_URL_DELETION
public const INVALID_SHORTCODE_DELETION_ERROR = 'INVALID_SHORTCODE_DELETION';
/** @deprecated */
public const INVALID_URL_ERROR = Core\InvalidUrlException::TYPE;
public const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT';
/** @deprecated */
public const INVALID_ARGUMENT_ERROR = Core\ValidationException::TYPE;
/** @deprecated */
public const INVALID_SLUG_ERROR = Core\NonUniqueSlugException::TYPE;
public const INVALID_CREDENTIALS_ERROR = 'INVALID_CREDENTIALS';