exceptionHandler example

'type' => $type, 'message' => $message, 'file' => $file, 'line' => $line] = $error;

        if ($this->exceptionCaughtByExceptionHandler) {
            $message .= "\n【Previous Exception】\n"
                . get_class($this->exceptionCaughtByExceptionHandler) . "\n"
                . $this->exceptionCaughtByExceptionHandler->getMessage() . "\n"
                . $this->exceptionCaughtByExceptionHandler->getTraceAsString();
        }

        if (in_array($type[E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], true)) {
            $this->exceptionHandler(new ErrorException($message, 0, $type$file$line));
        }
    }

    /** * Determines the view to display based on the exception thrown, * whether an HTTP or CLI request, etc. * * @return string The path and filename of the view file to use * * @deprecated 4.4.0 No longer used. Moved to ExceptionHandler. */
    

    else {
      $values = $this->fromQuery->getArguments();
    }

    $stmt = $this->connection->prepareStatement((string) $this$this->queryOptions);
    try {
      $stmt->execute($values$this->queryOptions);
      $last_insert_id = $this->connection->lastInsertId();
    }
    catch (\Exception $e) {
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt$values$this->queryOptions);
    }

    // Re-initialize the values array so that we can re-use this query.     $this->insertValues = [];

    return $last_insert_id;
  }

  public function __toString() {
    // Create a sanitized comment string to prepend to the query.     $comments = $this->connection->makeComment($this->comments);

    

  public function prepareStatement(string $query, array $options, bool $allow_row_count = FALSE): StatementInterface {
    if (isset($options['return'])) {
      @trigger_error('Passing "return" option to ' . __METHOD__ . '() is deprecated in drupal:9.4.0 and is removed in drupal:11.0.0. For data manipulation operations, use dynamic queries instead. See https://www.drupal.org/node/3185520', E_USER_DEPRECATED);
    }

    try {
      $query = $this->preprocessStatement($query$options);
      $statement = new Statement($this->connection, $this$query$options['pdo'] ?? []$allow_row_count);
    }
    catch (\Exception $e) {
      $this->exceptionHandler()->handleStatementException($e$query$options);
    }
    return $statement;
  }

  public function nextId($existing_id = 0) {
    try {
      $this->startTransaction();
    }
    catch (\PDOException $e) {
      // $this->exceptionHandler()->handleExecutionException()       // requires a $statement argument, so we cannot use that.
    // example, \Drupal\Core\Cache\DatabaseBackend.     $this->connection->addSavepoint();
    try {
      $stmt->execute(NULL, $this->queryOptions);
      if (isset($table_information->serial_fields[0])) {
        $last_insert_id = $stmt->fetchField();
      }
      $this->connection->releaseSavepoint();
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt[]$this->queryOptions);
    }

    // Re-initialize the values array so that we can re-use this query.     $this->insertValues = [];

    return $last_insert_id ?? NULL;
  }

  public function __toString() {
    // Create a sanitized comment string to prepend to the query.     $comments = $this->connection->makeComment($this->comments);

    

  public function prepareStatement(string $query, array $options, bool $allow_row_count = FALSE): StatementInterface {
    if (isset($options['return'])) {
      @trigger_error('Passing "return" option to ' . __METHOD__ . '() is deprecated in drupal:9.4.0 and is removed in drupal:11.0.0. For data manipulation operations, use dynamic queries instead. See https://www.drupal.org/node/3185520', E_USER_DEPRECATED);
    }

    try {
      $query = $this->preprocessStatement($query$options);
      $statement = new $this->statementWrapperClass($this$this->connection, $query$options['pdo'] ?? []$allow_row_count);
    }
    catch (\Exception $e) {
      $this->exceptionHandler()->handleStatementException($e$query$options);
    }

    return $statement;
  }

  /** * Returns a string SQL statement ready for preparation. * * This method replaces table names in curly braces and identifiers in square * brackets with platform specific replacements, appropriately escaping them * and wrapping them with platform quote characters. * * @param string $query * The query string as SQL, with curly braces surrounding the table names, * and square brackets surrounding identifiers. * @param array $options * An associative array of options to control how the query is run. See * the documentation for self::defaultOptions() for details. * * @return string * A string SQL statement ready for preparation. * * @throws \InvalidArgumentException * If multiple statements are included in the string, and delimiters are * not allowed in the query. */
    if (count($this->insertFields)) {
      $stmt = $this->connection->prepareStatement((string) $this$this->queryOptions);

      if (count($this->insertValues) === 1) {
        // Inserting a single row does not require a transaction to be atomic,         // and executes faster without a transaction wrapper.         $insert_values = $this->insertValues[0];
        try {
          $stmt->execute($insert_values$this->queryOptions);
        }
        catch (\Exception $e) {
          $this->connection->exceptionHandler()->handleExecutionException($e$stmt$insert_values$this->queryOptions);
        }
      }
      else {
        // Inserting multiple rows requires a transaction to be atomic, and         // executes faster as a single transaction.         try {
          $transaction = $this->connection->startTransaction();
        }
        catch (\PDOException $e) {
          // $this->connection->exceptionHandler()->handleExecutionException()           // requires a $statement argument, so we cannot use that.

  public function execute() {
    $stmt = $this->connection->prepareStatement((string) $this$this->queryOptions, TRUE);
    try {
      $stmt->execute([]$this->queryOptions);
      return $stmt->rowCount();
    }
    catch (\Exception $e) {
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt[]$this->queryOptions);
    }

    return NULL;
  }

  /** * Implements PHP magic __toString method to convert the query to a string. * * @return string * The prepared statement. */
  

    }

    $this->connection->addSavepoint();
    try {
      $stmt->execute(NULL, $this->queryOptions);
      $this->connection->releaseSavepoint();
      return $stmt->rowCount();
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt[]$this->queryOptions);
    }
  }

}
if (count($this->condition)) {
      $this->condition->compile($this->connection, $this);
      $update_values = array_merge($update_values$this->condition->arguments());
    }

    $stmt = $this->connection->prepareStatement((string) $this$this->queryOptions, TRUE);
    try {
      $stmt->execute($update_values$this->queryOptions);
      return $stmt->rowCount();
    }
    catch (\Exception $e) {
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt$update_values$this->queryOptions);
    }
  }

  /** * Implements PHP magic __toString method to convert the query to a string. * * @return string * The prepared statement. */
  public function __toString() {
    // Create a sanitized comment string to prepend to the query.
foreach ($insert_values as $value) {
        $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
      }
    }

    $stmt = $this->connection->prepareStatement((string) $this$this->queryOptions, TRUE);
    try {
      $stmt->execute($values$this->queryOptions);
      $affected_rows = $stmt->rowCount();
    }
    catch (\Exception $e) {
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt$values$this->queryOptions);
    }

    // Re-initialize the values array so that we can re-use this query.     $this->insertValues = [];

    return $affected_rows;
  }

}
    // mimic MySQL and SQLite transactions which don't fail if a single query     // fails. This is important for tables that are created on demand. For     // example, \Drupal\Core\Cache\DatabaseBackend.     $this->connection->addSavepoint();
    try {
      $stmt->execute(NULL, $options);
      $this->connection->releaseSavepoint();
      return $stmt->rowCount();
    }
    catch (\Exception $e) {
      $this->connection->rollbackSavepoint();
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt[]$options);
    }
  }

  /** * {@inheritdoc} */
  public function __toString() {
    // Create a sanitized comment string to prepend to the query.     $comments = $this->connection->makeComment($this->comments);

    // Default fields are always placed first for consistency.
if (count($this->condition)) {
      $this->condition->compile($this->connection, $this);
      $values = $this->condition->arguments();
    }

    $stmt = $this->connection->prepareStatement((string) $this$this->queryOptions, TRUE);
    try {
      $stmt->execute($values$this->queryOptions);
      return $stmt->rowCount();
    }
    catch (\Exception $e) {
      $this->connection->exceptionHandler()->handleExecutionException($e$stmt$values$this->queryOptions);
    }
  }

  /** * Implements PHP magic __toString method to convert the query to a string. * * @return string * The prepared statement. */
  public function __toString() {
    // Create a sanitized comment string to prepend to the query.
Home | Imprint | This part of the site doesn't use cookies.