You are here

public function ExceptionHandler::handleExecutionException in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Database/ExceptionHandler.php \Drupal\Core\Database\ExceptionHandler::handleExecutionException()
  2. 10 core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php \Drupal\mysql\Driver\Database\mysql\ExceptionHandler::handleExecutionException()
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/ExceptionHandler.php \Drupal\Core\Database\ExceptionHandler::handleExecutionException()

Handles exceptions thrown during execution of statement objects.

Parameters

\Exception $exception: The exception to be handled.

\Drupal\Core\Database\StatementInterface $statement: The statement object requested to be executed.

array $arguments: An array of arguments for the prepared statement.

array $options: An associative array of options to control how the database operation is run.

Throws

\Drupal\Core\Database\DatabaseExceptionWrapper

\Drupal\Core\Database\IntegrityConstraintViolationException

1 method overrides ExceptionHandler::handleExecutionException()
ExceptionHandler::handleExecutionException in core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php
Handles exceptions thrown during execution of statement objects.

File

core/lib/Drupal/Core/Database/ExceptionHandler.php, line 56

Class

ExceptionHandler
Base Database exception handler class.

Namespace

Drupal\Core\Database

Code

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