3 namespace Drupal\Core\EventSubscriber;
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
7 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
8 use Symfony\Component\HttpKernel\KernelEvents;
11 * Utility base class for exception subscribers.
13 * A subscriber may extend this class and implement getHandledFormats() to
14 * indicate which request formats it will respond to. Then implement an on*()
15 * method for any error code (HTTP response code) that should be handled. For
16 * example, to handle a specific error code like 404 Not Found messages add the
20 * public function on404(GetResponseForExceptionEvent $event) {}
23 * To implement a fallback for the entire 4xx class of codes, implement the
27 * public function on4xx(GetResponseForExceptionEvent $event) {}
30 * That method should then call $event->setResponse() to set the response object
31 * for the exception. Alternatively, it may opt not to do so and then other
32 * listeners will have the opportunity to handle the exception.
34 * Note: Core provides several important exception listeners by default. In most
35 * cases, setting the priority of a contrib listener to the default of 0 will
36 * do what you expect and handle the exceptions you'd expect it to handle.
37 * If a custom priority is set, be aware of the following core-registered
40 * - Fast404ExceptionHtmlSubscriber: 200. This subscriber will return a
41 * minimalist, high-performance 404 page for HTML requests. It is not
42 * recommended to have a priority higher than this one as it will only slow
44 * - ExceptionLoggingSubscriber: 50. This subscriber logs all exceptions but
45 * does not handle them. Do not register a listener with a higher priority
46 * unless you want exceptions to not get logged, which makes debugging more
48 * - DefaultExceptionSubscriber: -256. The subscriber of last resort, this will
49 * provide generic handling for any exception. A listener with a lower
50 * priority will never get called.
52 * All other core-provided exception handlers have negative priorities so most
53 * module-provided listeners will naturally take precedence over them.
55 abstract class HttpExceptionSubscriberBase implements EventSubscriberInterface {
58 * Specifies the request formats this subscriber will respond to.
61 * An indexed array of the format machine names that this subscriber will
62 * attempt to process, such as "html" or "json". Returning an empty array
63 * will apply to all formats.
65 * @see \Symfony\Component\HttpFoundation\Request
67 abstract protected function getHandledFormats();
70 * Specifies the priority of all listeners in this class.
72 * The default priority is 1, which is very low. To have listeners that have
73 * a "first attempt" at handling exceptions return a higher priority.
76 * The event priority of this subscriber.
78 protected static function getPriority() {
83 * Handles errors for this subscriber.
85 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
86 * The event to process.
88 public function onException(GetResponseForExceptionEvent $event) {
89 $exception = $event->getException();
91 // Make the exception available for example when rendering a block.
92 $request = $event->getRequest();
93 $request->attributes->set('exception', $exception);
95 $handled_formats = $this->getHandledFormats();
97 $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
99 if ($exception instanceof HttpExceptionInterface && (empty($handled_formats) || in_array($format, $handled_formats))) {
100 $method = 'on' . $exception->getStatusCode();
101 // Keep just the leading number of the status code to produce either a
102 // on400 or a 500 method callback.
103 $method_fallback = 'on' . substr($exception->getStatusCode(), 0, 1) . 'xx';
104 // We want to allow the method to be called and still not set a response
105 // if it has additional filtering logic to determine when it will apply.
106 // It is therefore the method's responsibility to set the response on the
107 // event if appropriate.
108 if (method_exists($this, $method)) {
109 $this->$method($event);
111 elseif (method_exists($this, $method_fallback)) {
112 $this->$method_fallback($event);
118 * Registers the methods in this class that should be listeners.
121 * An array of event listener definitions.
123 public static function getSubscribedEvents() {
124 $events[KernelEvents::EXCEPTION][] = ['onException', static::getPriority()];