class ExceptionHandler

Same name in this branch
  1. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/ExceptionHandler.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\ExceptionHandler
  2. 10 core/lib/Drupal/Core/Database/Driver/mysql/ExceptionHandler.php \Drupal\Core\Database\Driver\mysql\ExceptionHandler
  3. 10 core/lib/Drupal/Core/Database/ExceptionHandler.php \Drupal\Core\Database\ExceptionHandler
Same name and namespace in other branches
  1. 9 core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php \Drupal\mysql\Driver\Database\mysql\ExceptionHandler
  2. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/ExceptionHandler.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\ExceptionHandler
  3. 9 core/lib/Drupal/Core/Database/Driver/mysql/ExceptionHandler.php \Drupal\Core\Database\Driver\mysql\ExceptionHandler
  4. 9 core/lib/Drupal/Core/Database/ExceptionHandler.php \Drupal\Core\Database\ExceptionHandler
  5. 11.x core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php \Drupal\mysql\Driver\Database\mysql\ExceptionHandler
  6. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/ExceptionHandler.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\ExceptionHandler
  7. 11.x core/lib/Drupal/Core/Database/ExceptionHandler.php \Drupal\Core\Database\ExceptionHandler
  8. 11.x core/modules/mysqli/src/Driver/Database/mysqli/ExceptionHandler.php \Drupal\mysqli\Driver\Database\mysqli\ExceptionHandler

MySql database exception handler class.

Hierarchy

Expanded class hierarchy of ExceptionHandler

1 file declares its use of ExceptionHandler
ExceptionHandler.php in core/lib/Drupal/Core/Database/Driver/mysql/ExceptionHandler.php
3 string references to 'ExceptionHandler'
Connection::exceptionHandler in core/lib/Drupal/Core/Database/Connection.php
Returns the database exceptions handler.
Connection::getDriverClass in core/lib/Drupal/Core/Database/Connection.php
Gets the driver-specific override class if any for the specified class.
ConnectionTest::providerGetDriverClass in core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
Data provider for testGetDriverClass().

File

core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php, line 16

Namespace

Drupal\mysql\Driver\Database\mysql
View source
class ExceptionHandler extends BaseExceptionHandler {
  
  /**
   * {@inheritdoc}
   */
  public function handleExecutionException(\Exception $exception, StatementInterface $statement, array $arguments = [], array $options = []) : void {
    if ($exception instanceof \PDOException) {
      // Wrap the exception in another exception, because PHP does not allow
      // overriding Exception::getMessage(). Its message is the extra database
      // debug information.
      $code = is_int($exception->getCode()) ? $exception->getCode() : 0;
      // If a max_allowed_packet error occurs the message length is truncated.
      // This should prevent the error from recurring if the exception is logged
      // to the database using dblog or the like.
      if (($exception->errorInfo[1] ?? NULL) === 1153) {
        $message = Unicode::truncateBytes($exception->getMessage(), Connection::MIN_MAX_ALLOWED_PACKET);
        throw new DatabaseExceptionWrapper($message, $code, $exception);
      }
      $message = $exception->getMessage() . ": " . $statement->getQueryString() . "; " . print_r($arguments, TRUE);
      // SQLSTATE 23xxx errors indicate an integrity constraint violation. Also,
      // in case of attempted INSERT of a record with an undefined column and no
      // default value indicated in schema, MySql returns a 1364 error code.
      if (substr($exception->getCode(), -6, -3) == '23' || ($exception->errorInfo[1] ?? NULL) === 1364) {
        throw new IntegrityConstraintViolationException($message, $code, $exception);
      }
      if ($exception->getCode() === '42000') {
        match ($exception->errorInfo[1]) {  1071 => throw new SchemaTableKeyTooLargeException($message, $code, $exception),
          1074 => throw new SchemaTableColumnSizeTooLargeException($message, $code, $exception),
          default => throw new DatabaseExceptionWrapper($message, 0, $exception),
        
        };
      }
      throw new DatabaseExceptionWrapper($message, 0, $exception);
    }
    throw $exception;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExceptionHandler::handleExecutionException public function Handles exceptions thrown during execution of statement objects. Overrides ExceptionHandler::handleExecutionException
ExceptionHandler::handleStatementException public function Handles exceptions thrown during the preparation of statement objects.

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