catchException example

/** * {@inheritdoc} */
  public function has($key) {
    try {
      return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key', [
        ':collection' => $this->collection,
        ':key' => $key,
      ])->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      return FALSE;
    }
  }

  /** * {@inheritdoc} */
  public function getMultiple(array $keys) {
    $values = [];
    try {
      $result = $this->connection->query('SELECT [name], [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [name] IN ( :keys[] ) AND [collection] = :collection', [':keys[]' => $keys, ':collection' => $this->collection])->fetchAllAssoc('name');
      
public function clear($name$identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->requestStack->getCurrentRequest()->getClientIp();
    }
    try {
      $this->connection->delete(static::TABLE_NAME)
        ->condition('event', $name)
        ->condition('identifier', $identifier)
        ->execute();
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
  }

  /** * {@inheritdoc} */
  public function clearByPrefix(string $name, string $prefix): void {
    try {
      $this->connection->delete(static::TABLE_NAME)
        ->condition('event', $name)
        ->condition('identifier', $prefix . '-%', 'LIKE')
        
/** * Deletes expired items. */
  public function garbageCollection() {
    try {
      $this->connection->delete('key_value_expire')
        ->condition('expire', REQUEST_TIME, '<')
        ->execute();
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
  }

  /** * Act on an exception when the table might not have been created. * * If the table does not yet exist, that's fine, but if the table exists and * yet the query failed, then the exception needs to propagate. * * @param \Exception $e * The exception. * * @throws \Exception */
/** * {@inheritdoc} */
  public function lockMayBeAvailable($name) {
    $name = $this->normalizeName($name);

    try {
      $lock = $this->database->query('SELECT [expire], [value] FROM {semaphore} WHERE [name] = :name', [':name' => $name])->fetchAssoc();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      // If the table does not exist yet then the lock may be available.       $lock = FALSE;
    }
    if (!$lock) {
      return TRUE;
    }
    $expire = (float) $lock['expire'];
    $now = microtime(TRUE);
    if ($now > $expire) {
      // We check two conditions to prevent a race condition where another       // request acquired the lock and set a new expire time. We add a small
      foreach (array_chunk($cids, 1000) as $cids_chunk) {
        $this->connection->delete($this->bin)
          ->condition('cid', $cids_chunk, 'IN')
          ->execute();
      }
    }
    catch (\Exception $e) {
      // Create the cache table, which will be empty. This fixes cases during       // core install where a cache table is cleared before it is set       // with {cache_render} and {cache_data}.       if (!$this->ensureBinExists()) {
        $this->catchException($e);
      }
    }
  }

  /** * {@inheritdoc} */
  public function deleteAll() {
    try {
      $this->connection->truncate($this->bin)->execute();
    }
    


  /** * {@inheritdoc} */
  public function numberOfItems() {
    try {
      return (int) $this->connection->query('SELECT COUNT([item_id]) FROM {' . static::TABLE_NAME . '} WHERE [name] = :name', [':name' => $this->name])
        ->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      // If there is no table there cannot be any items.       return 0;
    }
  }

  /** * {@inheritdoc} */
  public function claimItem($lease_time = 30) {
    // Claim an item by updating its expire fields. If claim is not successful     // another thread may have claimed the item in the meantime. Therefore loop

  public function claimItem($lease_time = 0) {
    try {
      $item = $this->connection->queryRange('SELECT [data], [item_id] FROM {queue} q WHERE [name] = :name ORDER BY [item_id] ASC', 0, 1, [':name' => $this->name])->fetchObject();
      if ($item) {
        $item->data = unserialize($item->data);
        return $item;
      }
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
    return FALSE;
  }

  /** * Retrieves all remaining items in the queue. * * This is specific to Batch API and is not part of the * \Drupal\Core\Queue\QueueInterface. * * @return array * An array of queue items. */

  public function has($key) {
    try {
      return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key AND [expire] > :now', [
        ':collection' => $this->collection,
        ':key' => $key,
        ':now' => REQUEST_TIME,
      ])->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      return FALSE;
    }
  }

  /** * {@inheritdoc} */
  public function getMultiple(array $keys) {
    try {
      $values = $this->connection->query(
        'SELECT [name], [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [expire] > :now AND [name] IN ( :keys[] ) AND [collection] = :collection',
        [

  public function load($id) {
    // Ensure that a session is started before using the CSRF token generator.     $this->session->start();
    try {
      $batch = $this->connection->query("SELECT [batch] FROM {batch} WHERE [bid] = :bid AND [token] = :token", [
        ':bid' => $id,
        ':token' => $this->csrfToken->get($id),
      ])->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      $batch = FALSE;
    }
    if ($batch) {
      return unserialize($batch);
    }
    return FALSE;
  }

  /** * {@inheritdoc} */
  
Home | Imprint | This part of the site doesn't use cookies.