1<?php 2 3namespace Drupal\Core\Database; 4 5/** 6 * Base Database exception handler class. 7 * 8 * This class handles exceptions thrown by the database layer. Database driver 9 * implementation can provide an alternative implementation to support special 10 * handling required by that database. 11 */ 12class ExceptionHandler { 13 14 /** 15 * Handles exceptions thrown during the preparation of statement objects. 16 * 17 * @param \Exception $exception 18 * The exception to be handled. 19 * @param string $sql 20 * The SQL statement that was requested to be prepared. 21 * @param array $options 22 * An associative array of options to control how the database operation is 23 * run. 24 * 25 * @throws \Drupal\Core\Database\DatabaseExceptionWrapper 26 */ 27 public function handleStatementException(\Exception $exception, string $sql, array $options = []): void { 28 if (array_key_exists('throw_exception', $options)) { 29 @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); 30 if (!($options['throw_exception'])) { 31 return; 32 } 33 } 34 35 if ($exception instanceof \PDOException) { 36 // Wrap the exception in another exception, because PHP does not allow 37 // overriding Exception::getMessage(). Its message is the extra database 38 // debug information. 39 $message = $exception->getMessage() . ": " . $sql . "; "; 40 throw new DatabaseExceptionWrapper($message, 0, $exception); 41 } 42 43 throw $exception; 44 } 45 46 /** 47 * Handles exceptions thrown during execution of statement objects. 48 * 49 * @param \Exception $exception 50 * The exception to be handled. 51 * @param \Drupal\Core\Database\StatementInterface $statement 52 * The statement object requested to be executed. 53 * @param array $arguments 54 * An array of arguments for the prepared statement. 55 * @param array $options 56 * An associative array of options to control how the database operation is 57 * run. 58 * 59 * @throws \Drupal\Core\Database\DatabaseExceptionWrapper 60 * @throws \Drupal\Core\Database\IntegrityConstraintViolationException 61 */ 62 public function handleExecutionException(\Exception $exception, StatementInterface $statement, array $arguments = [], array $options = []): void { 63 if (array_key_exists('throw_exception', $options)) { 64 @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); 65 if (!($options['throw_exception'])) { 66 return; 67 } 68 } 69 70 if ($exception instanceof \PDOException) { 71 // Wrap the exception in another exception, because PHP does not allow 72 // overriding Exception::getMessage(). Its message is the extra database 73 // debug information. 74 $message = $exception->getMessage() . ": " . $statement->getQueryString() . "; " . print_r($arguments, TRUE); 75 // Match all SQLSTATE 23xxx errors. 76 if (substr($exception->getCode(), -6, -3) == '23') { 77 throw new IntegrityConstraintViolationException($message, $exception->getCode(), $exception); 78 } 79 throw new DatabaseExceptionWrapper($message, 0, $exception); 80 } 81 82 throw $exception; 83 } 84 85} 86