|
8 | 8 |
|
9 | 9 | use Exception; |
10 | 10 | use Ibexa\Contracts\Core\Repository\ContentService; |
11 | | -use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; |
| 11 | +use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException as APINotFoundException; |
12 | 12 | use Ibexa\Contracts\Core\Repository\Exceptions\NotImplementedException; |
13 | 13 | use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException; |
14 | 14 | use Ibexa\Contracts\Core\Repository\LocationService; |
15 | 15 | use Ibexa\Contracts\Core\Repository\Values\Content\Content; |
16 | 16 | use Ibexa\Contracts\Core\Repository\Values\Content\Location; |
| 17 | +use Ibexa\Core\Base\Exceptions\BadStateException; |
17 | 18 | use Ibexa\Core\Helper\ContentPreviewHelper; |
18 | 19 | use Ibexa\Core\Helper\PreviewLocationProvider; |
19 | 20 | use Ibexa\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator; |
@@ -132,46 +133,19 @@ public function previewContentAction( |
132 | 133 | HttpKernelInterface::SUB_REQUEST, |
133 | 134 | false |
134 | 135 | ); |
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 | + ); |
164 | 142 | if ($this->debugMode) { |
165 | | - throw $e; |
| 143 | + throw new BadStateException($message, 1, $e); |
166 | 144 | } |
167 | 145 |
|
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 | 146 | return new Response($message); |
| 147 | + } catch (Exception $e) { |
| 148 | + return $this->buildResponseForGenericPreviewError($location, $content, $e); |
175 | 149 | } |
176 | 150 | $response->headers->addCacheControlDirective('no-cache', true); |
177 | 151 | $response->setPrivate(); |
@@ -228,6 +202,41 @@ protected function getForwardRequest( |
228 | 202 | $forwardRequestParameters |
229 | 203 | ); |
230 | 204 | } |
| 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 | + } |
231 | 240 | } |
232 | 241 |
|
233 | 242 | class_alias(PreviewController::class, 'eZ\Publish\Core\MVC\Symfony\Controller\Content\PreviewController'); |
0 commit comments