You are here

public function ExceptionHandler::handleExecutionException in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Database/ExceptionHandler.php \Drupal\Core\Database\ExceptionHandler::handleExecutionException()
  2. 9 core/lib/Drupal/Core/Database/Driver/mysql/ExceptionHandler.php \Drupal\Core\Database\Driver\mysql\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

Overrides ExceptionHandler::handleExecutionException

File

core/lib/Drupal/Core/Database/Driver/mysql/ExceptionHandler.php, line 19

Class

ExceptionHandler
MySql database exception handler class.

Namespace

Drupal\Core\Database\Driver\mysql

Code

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.
    $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);
    }
    throw new DatabaseExceptionWrapper($message, 0, $exception);
  }
  throw $exception;
}