Skip to main content
Tweeted twitter.com/StackDrupal/status/862669971901558784
removed "Drupal" from the title
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Handle Drupal Databasedatabase connection exception via custom handler

I want to catch Databasethe database connection refuse/denied/not found exceptions with a custom handler. I have already buildbuilt an handler which is overriding drupalthe default logger factory.

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

I can see mysqlthe connection driver uses the following code in open() function asopen().

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 corehandler. How can I achieve this?

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.

Handle database connection exception via custom handler

I want to catch the database connection exceptions with a custom handler. I have already built an handler which is overriding the default logger factory, but now I want to use that custom handler to log database exceptions, without hacking the core.

I can see the connection driver uses the following code in open().

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;
}

I am not sure how I can catch the exception and log it using the handler. How can I achieve this?

edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Source Link
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.