class ExceptionHandler

Same name in this branch
  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
Same name in other branches
  1. 10 core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php \Drupal\mysql\Driver\Database\mysql\ExceptionHandler
  2. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/ExceptionHandler.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\ExceptionHandler
  3. 10 core/lib/Drupal/Core/Database/Driver/mysql/ExceptionHandler.php \Drupal\Core\Database\Driver\mysql\ExceptionHandler
  4. 10 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

Base Database exception handler class.

This class handles exceptions thrown by the database layer of a PDO-based database connection. Database driver implementations can provide an alternative implementation to support special handling required by that database.

Hierarchy

Expanded class hierarchy of ExceptionHandler

2 files declare their use of ExceptionHandler
ExceptionHandler.php in core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php
ExceptionHandler.php in core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/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/lib/Drupal/Core/Database/ExceptionHandler.php, line 13

Namespace

Drupal\Core\Database
View source
class ExceptionHandler {
    
    /**
     * Handles exceptions thrown during the preparation of statement objects.
     *
     * @param \Exception $exception
     *   The exception to be handled.
     * @param string $sql
     *   The SQL statement that was requested to be prepared.
     * @param array $options
     *   An associative array of options to control how the database operation is
     *   run.
     *
     * @throws \Drupal\Core\Database\DatabaseExceptionWrapper
     */
    public function handleStatementException(\Exception $exception, string $sql, array $options = []) : void {
        if (array_key_exists('throw_exception', $options)) {
            @trigger_error('Passing a \'throw_exception\' option to ' . __METHOD__ . ' is deprecated in drupal:9.2.0 and is removed in drupal:10.0.0. Always catch exceptions. See https://www.drupal.org/node/3201187', E_USER_DEPRECATED);
            if (!$options['throw_exception']) {
                return;
            }
        }
        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.
            $message = $exception->getMessage() . ": " . $sql . "; ";
            throw new DatabaseExceptionWrapper($message, 0, $exception);
        }
        throw $exception;
    }
    
    /**
     * Handles exceptions thrown during execution of statement objects.
     *
     * @param \Exception $exception
     *   The exception to be handled.
     * @param \Drupal\Core\Database\StatementInterface $statement
     *   The statement object requested to be executed.
     * @param array $arguments
     *   An array of arguments for the prepared statement.
     * @param array $options
     *   An associative array of options to control how the database operation is
     *   run.
     *
     * @throws \Drupal\Core\Database\DatabaseExceptionWrapper
     * @throws \Drupal\Core\Database\IntegrityConstraintViolationException
     */
    public function handleExecutionException(\Exception $exception, StatementInterface $statement, array $arguments = [], array $options = []) : void {
        if (array_key_exists('throw_exception', $options)) {
            @trigger_error('Passing a \'throw_exception\' option to ' . __METHOD__ . ' is deprecated in drupal:9.2.0 and is removed in drupal:10.0.0. Always catch exceptions. See https://www.drupal.org/node/3201187', E_USER_DEPRECATED);
            if (!$options['throw_exception']) {
                return;
            }
        }
        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.
            $message = $exception->getMessage() . ": " . $statement->getQueryString() . "; " . print_r($arguments, TRUE);
            // Match all SQLSTATE 23xxx errors.
            if (substr($exception->getCode(), -6, -3) == '23') {
                throw new IntegrityConstraintViolationException($message, $exception->getCode(), $exception);
            }
            throw new DatabaseExceptionWrapper($message, 0, $exception);
        }
        throw $exception;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExceptionHandler::handleExecutionException public function Handles exceptions thrown during execution of statement objects. 1
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.