function HttpExceptionSubscriberBase::onException

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/EventSubscriber/HttpExceptionSubscriberBase.php \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase::onException()
  2. 10 core/lib/Drupal/Core/EventSubscriber/HttpExceptionSubscriberBase.php \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase::onException()
  3. 11.x core/lib/Drupal/Core/EventSubscriber/HttpExceptionSubscriberBase.php \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase::onException()

Handles errors for this subscriber.

Parameters

\Symfony\Component\HttpKernel\Event\ExceptionEvent $event: The event to process.

1 method overrides HttpExceptionSubscriberBase::onException()
DefaultExceptionSubscriber::onException in core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php
Handles errors for this subscriber.

File

core/lib/Drupal/Core/EventSubscriber/HttpExceptionSubscriberBase.php, line 88

Class

HttpExceptionSubscriberBase
Utility base class for exception subscribers.

Namespace

Drupal\Core\EventSubscriber

Code

public function onException(ExceptionEvent $event) {
    $exception = $event->getThrowable();
    // Make the exception available for example when rendering a block.
    $request = $event->getRequest();
    $request->attributes
        ->set('exception', $exception);
    $handled_formats = $this->getHandledFormats();
    $format = $request->query
        ->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat());
    if ($exception instanceof HttpExceptionInterface && (empty($handled_formats) || in_array($format, $handled_formats))) {
        $method = 'on' . $exception->getStatusCode();
        // Keep just the leading number of the status code to produce either a
        // 400 or a 500 method callback.
        $method_fallback = 'on' . substr($exception->getStatusCode(), 0, 1) . 'xx';
        // We want to allow the method to be called and still not set a response
        // if it has additional filtering logic to determine when it will apply.
        // It is therefore the method's responsibility to set the response on the
        // event if appropriate.
        if (method_exists($this, $method)) {
            $this->{$method}($event);
        }
        elseif (method_exists($this, $method_fallback)) {
            $this->{$method_fallback}($event);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.