13
13
use eZ \Publish \API \Repository \LocationService ;
14
14
use eZ \Publish \API \Repository \Values \Content \Content ;
15
15
use eZ \Publish \API \Repository \Values \Content \Location ;
16
+ use eZ \Publish \Core \Base \Exceptions \NotFoundException ;
16
17
use eZ \Publish \Core \Helper \ContentPreviewHelper ;
17
18
use eZ \Publish \Core \Helper \PreviewLocationProvider ;
18
19
use eZ \Publish \Core \MVC \Symfony \Routing \Generator \UrlAliasGenerator ;
21
22
use eZ \Publish \Core \MVC \Symfony \SiteAccess ;
22
23
use eZ \Publish \Core \MVC \Symfony \View \CustomLocationControllerChecker ;
23
24
use eZ \Publish \Core \MVC \Symfony \View \ViewManagerInterface ;
25
+ use Psr \Log \LoggerAwareTrait ;
26
+ use Psr \Log \LoggerInterface ;
27
+ use Psr \Log \NullLogger ;
24
28
use Symfony \Component \HttpFoundation \Request ;
29
+ use Symfony \Component \HttpFoundation \Response ;
25
30
use Symfony \Component \HttpKernel \HttpKernelInterface ;
26
31
use Symfony \Component \Security \Core \Authorization \AuthorizationCheckerInterface ;
27
32
use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
28
33
29
34
class PreviewController
30
35
{
36
+ use LoggerAwareTrait;
37
+
31
38
public const PREVIEW_PARAMETER_NAME = 'isPreview ' ;
32
39
public const CONTENT_VIEW_ROUTE = '_ez_content_view ' ;
33
40
@@ -52,14 +59,18 @@ class PreviewController
52
59
/** @var \eZ\Publish\Core\MVC\Symfony\View\CustomLocationControllerChecker */
53
60
private $ controllerChecker ;
54
61
62
+ private bool $ debugMode ;
63
+
55
64
public function __construct (
56
65
ContentService $ contentService ,
57
66
LocationService $ locationService ,
58
67
HttpKernelInterface $ kernel ,
59
68
ContentPreviewHelper $ previewHelper ,
60
69
AuthorizationCheckerInterface $ authorizationChecker ,
61
70
PreviewLocationProvider $ locationProvider ,
62
- CustomLocationControllerChecker $ controllerChecker
71
+ CustomLocationControllerChecker $ controllerChecker ,
72
+ bool $ debugMode ,
73
+ ?LoggerInterface $ logger = null ,
63
74
) {
64
75
$ this ->contentService = $ contentService ;
65
76
$ this ->locationService = $ locationService ;
@@ -68,6 +79,8 @@ public function __construct(
68
79
$ this ->authorizationChecker = $ authorizationChecker ;
69
80
$ this ->locationProvider = $ locationProvider ;
70
81
$ this ->controllerChecker = $ controllerChecker ;
82
+ $ this ->debugMode = $ debugMode ;
83
+ $ this ->logger = $ logger ?? new NullLogger ();
71
84
}
72
85
73
86
/**
@@ -118,17 +131,45 @@ public function previewContentAction(
118
131
false
119
132
);
120
133
} 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
124
138
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
125
139
<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>
126
140
EOF ;
127
141
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
+ }
129
158
} else {
159
+ $ this ->logger ->warning ('Unable to load the preview page ' , ['exception ' => $ e ]);
160
+ }
161
+
162
+ if ($ this ->debugMode ) {
130
163
throw $ e ;
131
164
}
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 );
132
173
}
133
174
$ response ->setPrivate ();
134
175
0 commit comments