3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Core\Cache\CacheableResponseInterface;
6 use Drupal\Core\Routing\RedirectDestinationInterface;
7 use Drupal\Core\Utility\Error;
8 use Psr\Log\LoggerInterface;
9 use Symfony\Component\HttpFoundation\Response;
10 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
11 use Symfony\Component\HttpKernel\HttpKernelInterface;
12 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
13 use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
16 * Exception subscriber for handling core default HTML error pages.
18 class DefaultExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
23 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
25 protected $httpKernel;
28 * The logger instance.
30 * @var \Psr\Log\LoggerInterface
35 * The redirect destination service.
37 * @var \Drupal\Core\Routing\RedirectDestinationInterface
39 protected $redirectDestination;
42 * A router implementation which does not check access.
44 * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface
46 protected $accessUnawareRouter;
49 * Constructs a new DefaultExceptionHtmlSubscriber.
51 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
53 * @param \Psr\Log\LoggerInterface $logger
55 * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
56 * The redirect destination service.
57 * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $access_unaware_router
58 * A router implementation which does not check access.
60 public function __construct(HttpKernelInterface $http_kernel, LoggerInterface $logger, RedirectDestinationInterface $redirect_destination, UrlMatcherInterface $access_unaware_router) {
61 $this->httpKernel = $http_kernel;
62 $this->logger = $logger;
63 $this->redirectDestination = $redirect_destination;
64 $this->accessUnawareRouter = $access_unaware_router;
70 protected static function getPriority() {
71 // A very low priority so that custom handlers are almost certain to fire
72 // before it, even if someone forgets to set a priority.
79 protected function getHandledFormats() {
84 * Handles a 4xx error for HTML.
86 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
87 * The event to process.
89 public function on4xx(GetResponseForExceptionEvent $event) {
90 if (($exception = $event->getException()) && $exception instanceof HttpExceptionInterface) {
91 $this->makeSubrequest($event, '/system/4xx', $exception->getStatusCode());
96 * Handles a 401 error for HTML.
98 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
99 * The event to process.
101 public function on401(GetResponseForExceptionEvent $event) {
102 $this->makeSubrequest($event, '/system/401', Response::HTTP_UNAUTHORIZED);
106 * Handles a 403 error for HTML.
108 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
109 * The event to process.
111 public function on403(GetResponseForExceptionEvent $event) {
112 $this->makeSubrequest($event, '/system/403', Response::HTTP_FORBIDDEN);
116 * Handles a 404 error for HTML.
118 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
119 * The event to process.
121 public function on404(GetResponseForExceptionEvent $event) {
122 $this->makeSubrequest($event, '/system/404', Response::HTTP_NOT_FOUND);
126 * Makes a subrequest to retrieve the default error page.
128 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
129 * The event to process.
131 * The path/url to which to make a subrequest for this error message.
132 * @param int $status_code
133 * The status code for the error being handled.
135 protected function makeSubrequest(GetResponseForExceptionEvent $event, $url, $status_code) {
136 $request = $event->getRequest();
137 $exception = $event->getException();
140 // Reuse the exact same request (so keep the same URL, keep the access
141 // result, the exception, et cetera) but override the routing information.
142 // This means that aside from routing, this is identical to the master
143 // request. This allows us to generate a response that is executed on
144 // behalf of the master request, i.e. for the original URL. This is what
145 // allows us to e.g. generate a 404 response for the original URL; if we
146 // would execute a subrequest with the 404 route's URL, then it'd be
147 // generated for *that* URL, not the *original* URL.
148 $sub_request = clone $request;
150 // The routing to the 404 page should be done as GET request because it is
151 // restricted to GET and POST requests only. Otherwise a DELETE request
152 // would for example trigger a method not allowed exception.
153 $request_context = clone ($this->accessUnawareRouter->getContext());
154 $request_context->setMethod('GET');
155 $this->accessUnawareRouter->setContext($request_context);
157 $sub_request->attributes->add($this->accessUnawareRouter->match($url));
159 // Add to query (GET) or request (POST) parameters:
160 // - 'destination' (to ensure e.g. the login form in a 403 response
161 // redirects to the original URL)
162 // - '_exception_statuscode'
163 $parameters = $sub_request->isMethod('GET') ? $sub_request->query : $sub_request->request;
164 $parameters->add($this->redirectDestination->getAsArray() + ['_exception_statuscode' => $status_code]);
166 $response = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
167 // Only 2xx responses should have their status code overridden; any
168 // other status code should be passed on: redirects (3xx), error (5xx)…
169 // @see https://www.drupal.org/node/2603788#comment-10504916
170 if ($response->isSuccessful()) {
171 $response->setStatusCode($status_code);
174 // Persist the exception's cacheability metadata, if any. If the exception
175 // itself isn't cacheable, then this will make the response uncacheable:
176 // max-age=0 will be set.
177 if ($response instanceof CacheableResponseInterface) {
178 $response->addCacheableDependency($exception);
181 // Persist any special HTTP headers that were set on the exception.
182 if ($exception instanceof HttpExceptionInterface) {
183 $response->headers->add($exception->getHeaders());
186 $event->setResponse($response);
188 catch (\Exception $e) {
189 // If an error happened in the subrequest we can't do much else. Instead,
190 // just log it. The DefaultExceptionSubscriber will catch the original
191 // exception and handle it normally.
192 $error = Error::decodeException($e);
193 $this->logger->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);