Skip to main content
1 of 3
arpitr
  • 7.9k
  • 2
  • 23
  • 48

Handle Drupal Database connection exception via custom handler

I want to catch Database connection refuse/denied/not found exceptions with custom handler. I have already build an handler which is overriding drupal default logger factory.

But now I want to use the custom handler to log database exceptions as mentioned without hacking the core.

I can see mysql connection driver code in open() function as

try {
  $pdo = new \PDO($dsn, $connection_options['username'], $connection_options['password'], $connection_options['pdo']);
}
catch (\PDOException $e) {
  if ($e->getCode() == static::DATABASE_NOT_FOUND) {
    throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e);
  }
  if ($e->getCode() == static::ACCESS_DENIED) {
    throw new DatabaseAccessDeniedException($e->getMessage(), $e->getCode(), $e);
  }
  throw $e;
}

But I am not sure how I can catch the exception and log it using custom handler without hacking the core.

arpitr
  • 7.9k
  • 2
  • 23
  • 48