Skip to content

Commit

Permalink
Refactored previewAction controller to improve error response
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Mar 7, 2024
1 parent c530f82 commit 1aae154
Showing 1 changed file with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -96,7 +97,7 @@ public function previewContentAction(
$language,
$siteAccessName = null,
?int $locationId = null
) {
): Response {
$this->previewHelper->setPreviewActive(true);

try {
Expand Down Expand Up @@ -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 = <<<EOF
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
<p>Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.</p>
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 = <<<EOF
<p>$message</p>
<p>Unable to load the preview page</p>
<p>See logs for more information</p>
EOF;

return new Response($message);
} catch (Exception $e) {
return $this->buildResponseForGenericPreviewError($location, $content, $e);
}

$response->setPrivate();

$this->previewHelper->restoreConfigScope();
Expand Down Expand Up @@ -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 = <<<EOF
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
<p>Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.</p>
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 .= <<<EOF
<p>Unable to load the preview page</p>
<p>See logs for more information</p>
EOF;

if ($this->debugMode) {
throw new BadStateException($message, 1, $e);
}

return new Response($message);
}
}

0 comments on commit 1aae154

Please sign in to comment.