Skip to content

Commit cc841e6

Browse files
alongoszvidarl
authored andcommitted
Refactored previewAction controller to improve error response (#404)
1 parent f872df9 commit cc841e6

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

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

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
use Exception;
1010
use Ibexa\Contracts\Core\Repository\ContentService;
11-
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
11+
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException as APINotFoundException;
1212
use Ibexa\Contracts\Core\Repository\Exceptions\NotImplementedException;
1313
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
1414
use Ibexa\Contracts\Core\Repository\LocationService;
1515
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
1616
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
17+
use Ibexa\Core\Base\Exceptions\BadStateException;
1718
use Ibexa\Core\Helper\ContentPreviewHelper;
1819
use Ibexa\Core\Helper\PreviewLocationProvider;
1920
use Ibexa\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
@@ -132,46 +133,19 @@ public function previewContentAction(
132133
HttpKernelInterface::SUB_REQUEST,
133134
false
134135
);
135-
} catch (\Exception $e) {
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
140-
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
141-
<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>
142-
EOF;
143-
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-
}
160-
} else {
161-
$this->logger->warning('Unable to load the preview page', ['exception' => $e]);
162-
}
163-
136+
} catch (APINotFoundException $e) {
137+
$message = 'Location not found or not available in requested language';
138+
$this->logger->warning(
139+
'Location not found or not available in requested language when loading the preview page',
140+
['exception' => $e]
141+
);
164142
if ($this->debugMode) {
165-
throw $e;
143+
throw new BadStateException($message, 1, $e);
166144
}
167145

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-
174146
return new Response($message);
147+
} catch (Exception $e) {
148+
return $this->buildResponseForGenericPreviewError($location, $content, $e);
175149
}
176150
$response->headers->addCacheControlDirective('no-cache', true);
177151
$response->setPrivate();
@@ -228,6 +202,41 @@ protected function getForwardRequest(
228202
$forwardRequestParameters
229203
);
230204
}
205+
206+
/**
207+
* @throws \Ibexa\Core\Base\Exceptions\BadStateException
208+
*/
209+
private function buildResponseForGenericPreviewError(Location $location, Content $content, Exception $e): Response
210+
{
211+
$message = '';
212+
try {
213+
if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
214+
$message = <<<EOF
215+
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
216+
<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>
217+
EOF;
218+
}
219+
} catch (Exception $innerException) {
220+
$message = 'An exception occurred when handling page preview exception';
221+
$this->logger->warning(
222+
'Unable to check if location uses a custom controller when loading the preview page',
223+
['exception' => $innerException]
224+
);
225+
}
226+
227+
$this->logger->warning('Unable to load the preview page', ['exception' => $e]);
228+
229+
$message .= <<<EOF
230+
<p>Unable to load the preview page</p>
231+
<p>See logs for more information</p>
232+
EOF;
233+
234+
if ($this->debugMode) {
235+
throw new BadStateException($message, 1, $e);
236+
}
237+
238+
return new Response($message);
239+
}
231240
}
232241

233242
class_alias(PreviewController::class, 'eZ\Publish\Core\MVC\Symfony\Controller\Content\PreviewController');

0 commit comments

Comments
 (0)