Skip to content

Commit 5267ac5

Browse files
committed
IBX-6312: View matcher ParentContentType should not throw execption if parent is not available
1 parent 19a4434 commit 5267ac5

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ services:
110110
$authorizationChecker: '@security.authorization_checker'
111111
$locationProvider: '@ezpublish.content_preview.location_provider'
112112
$controllerChecker: '@ezpublish.view.custom_location_controller_checker'
113+
$debugMode: '%kernel.debug%'
114+
$logger: '@logger'
113115
tags:
114116
- { name: controller.service_arguments }
115117

eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php

+46-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use eZ\Publish\API\Repository\LocationService;
1414
use eZ\Publish\API\Repository\Values\Content\Content;
1515
use eZ\Publish\API\Repository\Values\Content\Location;
16+
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
1617
use eZ\Publish\Core\Helper\ContentPreviewHelper;
1718
use eZ\Publish\Core\Helper\PreviewLocationProvider;
1819
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
@@ -21,13 +22,19 @@
2122
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
2223
use eZ\Publish\Core\MVC\Symfony\View\CustomLocationControllerChecker;
2324
use eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface;
25+
use Psr\Log\LoggerAwareTrait;
26+
use Psr\Log\LoggerInterface;
27+
use Psr\Log\NullLogger;
2428
use Symfony\Component\HttpFoundation\Request;
29+
use Symfony\Component\HttpFoundation\Response;
2530
use Symfony\Component\HttpKernel\HttpKernelInterface;
2631
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
2732
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2833

2934
class PreviewController
3035
{
36+
use LoggerAwareTrait;
37+
3138
public const PREVIEW_PARAMETER_NAME = 'isPreview';
3239
public const CONTENT_VIEW_ROUTE = '_ez_content_view';
3340

@@ -52,14 +59,18 @@ class PreviewController
5259
/** @var \eZ\Publish\Core\MVC\Symfony\View\CustomLocationControllerChecker */
5360
private $controllerChecker;
5461

62+
private bool $debugMode;
63+
5564
public function __construct(
5665
ContentService $contentService,
5766
LocationService $locationService,
5867
HttpKernelInterface $kernel,
5968
ContentPreviewHelper $previewHelper,
6069
AuthorizationCheckerInterface $authorizationChecker,
6170
PreviewLocationProvider $locationProvider,
62-
CustomLocationControllerChecker $controllerChecker
71+
CustomLocationControllerChecker $controllerChecker,
72+
bool $debugMode,
73+
?LoggerInterface $logger = null,
6374
) {
6475
$this->contentService = $contentService;
6576
$this->locationService = $locationService;
@@ -68,6 +79,8 @@ public function __construct(
6879
$this->authorizationChecker = $authorizationChecker;
6980
$this->locationProvider = $locationProvider;
7081
$this->controllerChecker = $controllerChecker;
82+
$this->debugMode = $debugMode;
83+
$this->logger = $logger ?? new NullLogger();
7184
}
7285

7386
/**
@@ -118,17 +131,45 @@ public function previewContentAction(
118131
false
119132
);
120133
} catch (\Exception $e) {
121-
if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
122-
// @todo This should probably be an exception that embeds the original one
123-
$message = <<<EOF
134+
try {
135+
if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
136+
// @todo This should probably be an exception that embeds the original one
137+
$message = <<<EOF
124138
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
125139
<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>
126140
EOF;
127141

128-
throw new Exception($message, 0, $e);
142+
throw new Exception($message, 0, $e);
143+
}
144+
} catch (\Exception $e2) {
145+
$this->logger->warning('Unable to check if location uses a custom controller when loading the preview page', ['exception' => $e2]);
146+
if ($this->debugMode) {
147+
throw $e2;
148+
}
149+
}
150+
$message = '';
151+
152+
if ($e instanceof NotFoundException) {
153+
$message .= 'Location not found or not available in requested language';
154+
$this->logger->warning('Location not found or not available in requested language when loading the preview page', ['exception' => $e]);
155+
if ($this->debugMode) {
156+
throw new Exception($message, 0, $e);
157+
}
129158
} else {
159+
$this->logger->warning('Unable to load the preview page', ['exception' => $e]);
160+
}
161+
162+
if ($this->debugMode) {
130163
throw $e;
131164
}
165+
166+
$message = <<<EOF
167+
<p>$message</p>
168+
<p>Unable to load the preview page</p>
169+
<p>See logs for more information</p>
170+
EOF;
171+
172+
return new Response($message);
132173
}
133174
$response->setPrivate();
134175

0 commit comments

Comments
 (0)