From d60e020e20a18106441155fc5b9b49408ffda3a7 Mon Sep 17 00:00:00 2001
From: Andrew Longosz
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); + } }