ExceptionHandler.php

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

Namespace

Drupal\Core\Database

File

core/lib/Drupal/Core/Database/ExceptionHandler.php

View source
<?php

namespace Drupal\Core\Database;


/**
 * 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.
 */
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 ($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 ($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;
    }

}

Classes

Title Deprecated Summary
ExceptionHandler Base Database exception handler class.

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