1

I'm trying to replicate a solution I had working on Drupal 7 into Drupal 10. I was able to change the http response code to 504 such that Cloudflare would return a cached site copy in the instance of the database server being upgraded. I've got this far - using an Exception event subscriber - but my code never gets called. I've added the Request debug method - and that fires - I see those logs. However, when I turn the DB off - I just get the core FinalExceptionSubscriber firing. Could it be that my module never gets loaded during the bootstrap process?

<?php

namespace Drupal\neway_platform\EventSubscriber;

use Drupal\Core\Database\DatabaseConnectionRefusedException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;


class NewayExceptionSubscriber implements EventSubscriberInterface {

  /**
   * Handles exceptions for this subscriber.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function onException(ExceptionEvent $event) {
    //
    error_log('Error here: ');
    $exception = $event->getThrowable();
    //$this->logger->get('php')->error('Custom logging message' . $exception->getMessage());
    if ($exception instanceof DatabaseConnectionRefusedException) {
      //$this->logger->get('php')->error('Custom logging message');
      $response = new Response('Site is being upgraded - please check back soon', 504);
      $event->setResponse($response);
    }
  }

  public function onRequest() {

    error_log('Your message here2: ');

  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    // Listen on 4xx exceptions late, but before the final exception handler.
    $events[KernelEvents::EXCEPTION][] = ['onException', 1000];
    $events[KernelEvents::REQUEST][] = ['onRequest', 1000];
    return $events;
  }

mymodule.services.yml

services:
  cron_fire_forget:
    class: '\Drupal\neway_platform\Service\CronFireForget'
  neway_exception:
    class: Drupal\neway_platform\EventSubscriber\NewayExceptionSubscriber
    tags:
      - { name: 'event_subscriber' }

logs:


    [02-Oct-2024 14:08:01 Europe/Berlin] Your message here2:
    [02-Oct-2024 14:08:01 Europe/Berlin] Your message here2:
    [02-Oct-2024 14:08:01 Europe/Berlin] Your message here2:
    [02-Oct-2024 14:08:20 Europe/Berlin] Drupal\Core\Database\DatabaseConnectionRefusedException: SQLST (edited) 
3
  • Drupal core subscribes to the same even in AuthenticationSubscriber. If the event handler is not called, there must be an issue somewhere else.
    – avpaderno
    Commented Oct 3 at 7:15
  • I've since found the Custom error page module - and they seem to have a similar issue with database connectivity functionality. drupal.org/project/error_page/issues/3273561 Commented Oct 4 at 14:12
  • 1
    Without the database, Drupal cannot create your event subscriber (which is a service). That explains why it is not invoked.
    – avpaderno
    Commented Oct 6 at 14:13

1 Answer 1

0

This does not seem possible using an EventSubscriber. Alternative is to change the core errors.inc file (ideally with a patch file if we have to got this route) - as even the core catch all Event Subscriber is not called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.