Skip to content

Commit f872df9

Browse files
committed
IBX-6312: View matcher ParentContentType should not throw execption if parent is not available
1 parent c338dc5 commit f872df9

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/bundle/Core/Resources/config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ services:
110110
$authorizationChecker: "@security.authorization_checker"
111111
$locationProvider: '@Ibexa\Core\Helper\PreviewLocationProvider'
112112
$controllerChecker: '@Ibexa\Core\MVC\Symfony\View\CustomLocationControllerChecker'
113+
$debugMode: '%kernel.debug%'
114+
$logger: '@logger'
113115
tags:
114116
- { name: controller.service_arguments }
115117

src/lib/MVC/Symfony/Controller/Content/PreviewController.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Exception;
1010
use Ibexa\Contracts\Core\Repository\ContentService;
11+
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
1112
use Ibexa\Contracts\Core\Repository\Exceptions\NotImplementedException;
1213
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
1314
use Ibexa\Contracts\Core\Repository\LocationService;
@@ -21,6 +22,9 @@
2122
use Ibexa\Core\MVC\Symfony\SiteAccess;
2223
use Ibexa\Core\MVC\Symfony\View\CustomLocationControllerChecker;
2324
use Ibexa\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;
2529
use Symfony\Component\HttpFoundation\Response;
2630
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -29,6 +33,8 @@
2933

3034
class PreviewController
3135
{
36+
use LoggerAwareTrait;
37+
3238
public const PREVIEW_PARAMETER_NAME = 'isPreview';
3339
public const CONTENT_VIEW_ROUTE = 'ibexa.content.view';
3440

@@ -53,14 +59,19 @@ class PreviewController
5359
/** @var \Ibexa\Core\MVC\Symfony\View\CustomLocationControllerChecker */
5460
private $controllerChecker;
5561

62+
/** @var bool */
63+
private $debugMode;
64+
5665
public function __construct(
5766
ContentService $contentService,
5867
LocationService $locationService,
5968
HttpKernelInterface $kernel,
6069
ContentPreviewHelper $previewHelper,
6170
AuthorizationCheckerInterface $authorizationChecker,
6271
PreviewLocationProvider $locationProvider,
63-
CustomLocationControllerChecker $controllerChecker
72+
CustomLocationControllerChecker $controllerChecker,
73+
bool $debugMode,
74+
?LoggerInterface $logger = null
6475
) {
6576
$this->contentService = $contentService;
6677
$this->locationService = $locationService;
@@ -69,6 +80,8 @@ public function __construct(
6980
$this->authorizationChecker = $authorizationChecker;
7081
$this->locationProvider = $locationProvider;
7182
$this->controllerChecker = $controllerChecker;
83+
$this->debugMode = $debugMode;
84+
$this->logger = $logger ?? new NullLogger();
7285
}
7386

7487
/**
@@ -120,17 +133,45 @@ public function previewContentAction(
120133
false
121134
);
122135
} catch (\Exception $e) {
123-
if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
124-
// @todo This should probably be an exception that embeds the original one
125-
$message = <<<EOF
136+
try {
137+
if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
138+
// @todo This should probably be an exception that embeds the original one
139+
$message = <<<EOF
126140
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
127141
<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>
128142
EOF;
129143

130-
throw new Exception($message, 0, $e);
144+
throw new Exception($message, 0, $e);
145+
}
146+
} catch (\Exception $e2) {
147+
$this->logger->warning('Unable to check if location uses a custom controller when loading the preview page', ['exception' => $e2]);
148+
if ($this->debugMode) {
149+
throw $e2;
150+
}
151+
}
152+
$message = '';
153+
154+
if ($e instanceof NotFoundException) {
155+
$message .= 'Location not found or not available in requested language';
156+
$this->logger->warning('Location not found or not available in requested language when loading the preview page', ['exception' => $e]);
157+
if ($this->debugMode) {
158+
throw new Exception($message, 0, $e);
159+
}
131160
} else {
161+
$this->logger->warning('Unable to load the preview page', ['exception' => $e]);
162+
}
163+
164+
if ($this->debugMode) {
132165
throw $e;
133166
}
167+
168+
$message = <<<EOF
169+
<p>$message</p>
170+
<p>Unable to load the preview page</p>
171+
<p>See logs for more information</p>
172+
EOF;
173+
174+
return new Response($message);
134175
}
135176
$response->headers->addCacheControlDirective('no-cache', true);
136177
$response->setPrivate();

tests/lib/MVC/Symfony/Controller/Controller/Content/PreviewControllerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ protected function getPreviewController(): PreviewController
7373
$this->previewHelper,
7474
$this->authorizationChecker,
7575
$this->locationProvider,
76-
$this->controllerChecker
76+
$this->controllerChecker,
77+
false,
78+
null
7779
);
7880
}
7981

0 commit comments

Comments
 (0)