diff --git a/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php b/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php
index afeee1edba..d37c06faf0 100644
--- a/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php
+++ b/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php
@@ -8,12 +8,13 @@
use Exception;
use eZ\Publish\API\Repository\ContentService;
+use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
-use eZ\Publish\Core\Base\Exceptions\NotFoundException;
+use eZ\Publish\Core\Base\Exceptions\BadStateException;
use eZ\Publish\Core\Helper\ContentPreviewHelper;
use eZ\Publish\Core\Helper\PreviewLocationProvider;
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
@@ -87,7 +88,7 @@ public function __construct(
/**
* @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException If Content is missing location as this is not supported in current version
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
- * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
+ * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
*/
public function previewContentAction(
Request $request,
@@ -96,7 +97,7 @@ public function previewContentAction(
$language,
$siteAccessName = null,
?int $locationId = null
- ) {
+ ): Response {
$this->previewHelper->setPreviewActive(true);
try {
@@ -131,47 +132,21 @@ public function previewContentAction(
HttpKernelInterface::SUB_REQUEST,
false
);
- } catch (\Exception $e) {
- try {
- if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
- // @todo This should probably be an exception that embeds the original one
- $message = <<
Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.
-EOF; - - throw new Exception($message, 0, $e); - } - } catch (\Exception $e2) { - $this->logger->warning('Unable to check if location uses a custom controller when loading the preview page', ['exception' => $e2]); - if ($this->debugMode) { - throw $e2; - } - } - $message = ''; - - if ($e instanceof NotFoundException) { - $message .= 'Location not found or not available in requested language'; - $this->logger->warning('Location not found or not available in requested language when loading the preview page', ['exception' => $e]); - if ($this->debugMode) { - throw new Exception($message, 0, $e); - } - } else { - $this->logger->warning('Unable to load the preview page', ['exception' => $e]); - } - + } catch (APINotFoundException $e) { + $message = 'Location not found or not available in requested language'; + $this->logger->warning( + 'Location not found or not available in requested language when loading the preview page', + ['exception' => $e] + ); if ($this->debugMode) { - throw $e; + throw new BadStateException($message, 1, $e); } - $message = <<Unable to load the preview page
-See logs for more information
-EOF; - return new Response($message); + } catch (Exception $e) { + return $this->buildResponseForGenericPreviewError($location, $content, $e); } + $response->setPrivate(); $this->previewHelper->restoreConfigScope(); @@ -228,4 +203,39 @@ protected function getForwardRequest(Location $location, Content $content, SiteA $forwardRequestParameters ); } + + /** + * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException + */ + private function buildResponseForGenericPreviewError(Location $location, Content $content, Exception $e): Response + { + $message = ''; + try { + if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) { + $message = <<Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.
+EOF; + } + } catch (Exception $innerException) { + $message = 'An exception occurred when handling page preview exception'; + $this->logger->warning( + 'Unable to check if location uses a custom controller when loading the preview page', + ['exception' => $innerException] + ); + } + + $this->logger->warning('Unable to load the preview page', ['exception' => $e]); + + $message .= <<See logs for more information
+EOF; + + if ($this->debugMode) { + throw new BadStateException($message, 1, $e); + } + + return new Response($message); + } }