Chris@0: setResponse() to set the response object Chris@0: * for the exception. Alternatively, it may opt not to do so and then other Chris@0: * listeners will have the opportunity to handle the exception. Chris@0: * Chris@0: * Note: Core provides several important exception listeners by default. In most Chris@0: * cases, setting the priority of a contrib listener to the default of 0 will Chris@0: * do what you expect and handle the exceptions you'd expect it to handle. Chris@0: * If a custom priority is set, be aware of the following core-registered Chris@0: * listeners. Chris@0: * Chris@0: * - Fast404ExceptionHtmlSubscriber: 200. This subscriber will return a Chris@0: * minimalist, high-performance 404 page for HTML requests. It is not Chris@0: * recommended to have a priority higher than this one as it will only slow Chris@0: * down that use case. Chris@0: * - ExceptionLoggingSubscriber: 50. This subscriber logs all exceptions but Chris@0: * does not handle them. Do not register a listener with a higher priority Chris@0: * unless you want exceptions to not get logged, which makes debugging more Chris@0: * difficult. Chris@0: * - DefaultExceptionSubscriber: -256. The subscriber of last resort, this will Chris@0: * provide generic handling for any exception. A listener with a lower Chris@0: * priority will never get called. Chris@0: * Chris@0: * All other core-provided exception handlers have negative priorities so most Chris@0: * module-provided listeners will naturally take precedence over them. Chris@0: */ Chris@0: abstract class HttpExceptionSubscriberBase implements EventSubscriberInterface { Chris@0: Chris@0: /** Chris@0: * Specifies the request formats this subscriber will respond to. Chris@0: * Chris@0: * @return array Chris@0: * An indexed array of the format machine names that this subscriber will Chris@0: * attempt to process, such as "html" or "json". Returning an empty array Chris@0: * will apply to all formats. Chris@0: * Chris@0: * @see \Symfony\Component\HttpFoundation\Request Chris@0: */ Chris@0: abstract protected function getHandledFormats(); Chris@0: Chris@0: /** Chris@0: * Specifies the priority of all listeners in this class. Chris@0: * Chris@0: * The default priority is 1, which is very low. To have listeners that have Chris@0: * a "first attempt" at handling exceptions return a higher priority. Chris@0: * Chris@0: * @return int Chris@0: * The event priority of this subscriber. Chris@0: */ Chris@0: protected static function getPriority() { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Handles errors for this subscriber. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function onException(GetResponseForExceptionEvent $event) { Chris@0: $exception = $event->getException(); Chris@0: Chris@0: // Make the exception available for example when rendering a block. Chris@0: $request = $event->getRequest(); Chris@0: $request->attributes->set('exception', $exception); Chris@0: Chris@0: $handled_formats = $this->getHandledFormats(); Chris@0: Chris@0: $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat()); Chris@0: Chris@0: if ($exception instanceof HttpExceptionInterface && (empty($handled_formats) || in_array($format, $handled_formats))) { Chris@0: $method = 'on' . $exception->getStatusCode(); Chris@0: // Keep just the leading number of the status code to produce either a Chris@0: // on400 or a 500 method callback. Chris@0: $method_fallback = 'on' . substr($exception->getStatusCode(), 0, 1) . 'xx'; Chris@0: // We want to allow the method to be called and still not set a response Chris@0: // if it has additional filtering logic to determine when it will apply. Chris@0: // It is therefore the method's responsibility to set the response on the Chris@0: // event if appropriate. Chris@0: if (method_exists($this, $method)) { Chris@0: $this->$method($event); Chris@0: } Chris@0: elseif (method_exists($this, $method_fallback)) { Chris@0: $this->$method_fallback($event); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers the methods in this class that should be listeners. Chris@0: * Chris@0: * @return array Chris@0: * An array of event listener definitions. Chris@0: */ Chris@0: public static function getSubscribedEvents() { Chris@0: $events[KernelEvents::EXCEPTION][] = ['onException', static::getPriority()]; Chris@0: return $events; Chris@0: } Chris@0: Chris@0: }