88
99use Exception ;
1010use Ibexa \Contracts \Core \Repository \ContentService ;
11+ use Ibexa \Contracts \Core \Repository \Exceptions \NotFoundException ;
1112use Ibexa \Contracts \Core \Repository \Exceptions \NotImplementedException ;
1213use Ibexa \Contracts \Core \Repository \Exceptions \UnauthorizedException ;
1314use Ibexa \Contracts \Core \Repository \LocationService ;
2122use Ibexa \Core \MVC \Symfony \SiteAccess ;
2223use Ibexa \Core \MVC \Symfony \View \CustomLocationControllerChecker ;
2324use Ibexa \Core \MVC \Symfony \View \ViewManagerInterface ;
25+ use Psr \Log \LoggerAwareTrait ;
26+ use Psr \Log \LoggerInterface ;
27+ use Psr \Log \NullLogger ;
2428use Symfony \Component \HttpFoundation \Request ;
2529use Symfony \Component \HttpFoundation \Response ;
2630use Symfony \Component \HttpKernel \HttpKernelInterface ;
2933
3034class 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>
128142EOF ;
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 ();
0 commit comments