Replaced hardcoded exceptions concatenations by PSR approach

This commit is contained in:
Alejandro Celaya 2018-10-20 12:50:10 +02:00
parent 2eca0da852
commit 787b791651
10 changed files with 20 additions and 19 deletions

View file

@ -75,7 +75,7 @@ abstract class AbstractTrackingAction implements MiddlewareInterface
return $this->createResp($url->getLongUrl());
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) {
$this->logger->warning('An error occurred while tracking short code.' . PHP_EOL . $e);
$this->logger->warning('An error occurred while tracking short code. {e}', ['e' => $e]);
return $this->buildErrorResponse($request, $handler);
}
}

View file

@ -63,7 +63,7 @@ class PreviewAction implements MiddlewareInterface
$imagePath = $this->previewGenerator->generatePreview($url->getLongUrl());
return $this->generateImageResponse($imagePath);
} catch (InvalidShortCodeException | EntityDoesNotExistException | PreviewGenerationException $e) {
$this->logger->warning('An error occurred while generating preview image.' . PHP_EOL . $e);
$this->logger->warning('An error occurred while generating preview image. {e}', ['e' => $e]);
return $this->buildErrorResponse($request, $handler);
}
}

View file

@ -67,7 +67,7 @@ class QrCodeAction implements MiddlewareInterface
try {
$this->urlShortener->shortCodeToUrl($shortCode);
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) {
$this->logger->warning('An error occurred while creating QR code' . PHP_EOL . $e);
$this->logger->warning('An error occurred while creating QR code. {e}', ['e' => $e]);
return $this->buildErrorResponse($request, $handler);
}

View file

@ -57,7 +57,7 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
$longUrl = $shortUrlData->getLongUrl();
$customSlug = $shortUrlMeta->getCustomSlug();
} catch (InvalidArgumentException $e) {
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
$this->logger->warning('Provided data is invalid. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
'message' => $e->getMessage(),
@ -77,7 +77,7 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
return new JsonResponse($transformer->transform($shortUrl));
} catch (InvalidUrlException $e) {
$this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e);
$this->logger->warning('Provided Invalid URL. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => \sprintf(
@ -86,7 +86,7 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
),
], self::STATUS_BAD_REQUEST);
} catch (NonUniqueSlugException $e) {
$this->logger->warning('Provided non-unique slug.' . PHP_EOL . $e);
$this->logger->warning('Provided non-unique slug. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => \sprintf(
@ -95,7 +95,7 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
),
], self::STATUS_BAD_REQUEST);
} catch (\Throwable $e) {
$this->logger->error('Unexpected error creating short url.' . PHP_EOL . $e);
$this->logger->error('Unexpected error creating short url. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::UNKNOWN_ERROR,
'message' => $this->translator->translate('Unexpected error occurred'),

View file

@ -50,14 +50,15 @@ class DeleteShortUrlAction extends AbstractRestAction
return new EmptyResponse();
} catch (Exception\InvalidShortCodeException $e) {
$this->logger->warning(
\sprintf('Provided short code %s does not belong to any URL.', $shortCode) . PHP_EOL . $e
'Provided short code {shortCode} does not belong to any URL. {e}',
['e' => $e, 'shortCode' => $shortCode]
);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
], self::STATUS_NOT_FOUND);
} catch (Exception\DeleteShortUrlException $e) {
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
$this->logger->warning('Provided data is invalid. {e}', ['e' => $e]);
$messagePlaceholder = $this->translator->translate(
'It is not possible to delete URL with short code "%s" because it has reached more than "%s" visits.'
);

View file

@ -60,13 +60,13 @@ class EditShortUrlAction extends AbstractRestAction
);
return new EmptyResponse();
} catch (Exception\InvalidShortCodeException $e) {
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
$this->logger->warning('Provided data is invalid. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
], self::STATUS_NOT_FOUND);
} catch (Exception\ValidationException $e) {
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
$this->logger->warning('Provided data is invalid. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => $this->translator->translate('Provided data is invalid.'),

View file

@ -60,7 +60,7 @@ class ListShortUrlsAction extends AbstractRestAction
$this->domainConfig
))]);
} catch (\Exception $e) {
$this->logger->error('Unexpected error while listing short URLs.' . PHP_EOL . $e);
$this->logger->error('Unexpected error while listing short URLs. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::UNKNOWN_ERROR,
'message' => $this->translator->translate('Unexpected error occurred'),

View file

@ -59,7 +59,7 @@ class ResolveShortUrlAction extends AbstractRestAction
$url = $this->urlShortener->shortCodeToUrl($shortCode);
return new JsonResponse($transformer->transform($url));
} catch (InvalidShortCodeException $e) {
$this->logger->warning('Provided short code with invalid format.' . PHP_EOL . $e);
$this->logger->warning('Provided short code with invalid format. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => \sprintf(
@ -68,13 +68,13 @@ class ResolveShortUrlAction extends AbstractRestAction
),
], self::STATUS_BAD_REQUEST);
} catch (EntityDoesNotExistException $e) {
$this->logger->warning('Provided short code couldn\'t be found.' . PHP_EOL . $e);
$this->logger->warning('Provided short code couldn\'t be found. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
], self::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Unexpected error while resolving the URL behind a short code.' . PHP_EOL . $e);
$this->logger->error('Unexpected error while resolving the URL behind a short code. {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::UNKNOWN_ERROR,
'message' => $this->translator->translate('Unexpected error occurred'),

View file

@ -59,7 +59,7 @@ class GetVisitsAction extends AbstractRestAction
],
]);
} catch (InvalidArgumentException $e) {
$this->logger->warning('Provided nonexistent short code' . PHP_EOL . $e);
$this->logger->warning('Provided nonexistent short code {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => sprintf(
@ -68,7 +68,7 @@ class GetVisitsAction extends AbstractRestAction
),
], self::STATUS_NOT_FOUND);
} catch (\Exception $e) {
$this->logger->error('Unexpected error while parsing short code' . PHP_EOL . $e);
$this->logger->error('Unexpected error while parsing short code {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::UNKNOWN_ERROR,
'message' => $this->translator->translate('Unexpected error occurred'),

View file

@ -80,7 +80,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa
try {
$plugin = $this->requestToAuthPlugin->fromRequest($request);
} catch (ContainerExceptionInterface | NoAuthenticationException $e) {
$this->logger->warning('Invalid or no authentication provided.' . PHP_EOL . $e);
$this->logger->warning('Invalid or no authentication provided. {e}', ['e' => $e]);
return $this->createErrorResponse(sprintf($this->translator->translate(
'Expected one of the following authentication headers, but none were provided, ["%s"]'
), implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)));
@ -91,7 +91,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa
$response = $handler->handle($request);
return $plugin->update($request, $response);
} catch (VerifyAuthenticationException $e) {
$this->logger->warning('Authentication verification failed.' . PHP_EOL . $e);
$this->logger->warning('Authentication verification failed. {e}', ['e' => $e]);
return $this->createErrorResponse($e->getPublicMessage(), $e->getErrorCode());
}
}