Kdyby\Doctrine\Connection::resolveException PHP Method

resolveException() public method

Deprecation:
public resolveException ( Exception | Throwable $e, string $query = NULL, array $params = [] ) : DBALException
$e Exception | Throwable
$query string
$params array
return DBALException
    public function resolveException($e, $query = NULL, $params = [])
    {
        if ($this->throwOldKdybyExceptions !== TRUE) {
            return $e;
        }
        if ($e instanceof Doctrine\DBAL\DBALException && ($pe = $e->getPrevious()) instanceof \PDOException) {
            $info = $pe->errorInfo;
        } elseif ($e instanceof \PDOException) {
            $info = $e->errorInfo;
        } else {
            return new DBALException($e, $query, $params, $this);
        }
        if ($this->getDriver() instanceof Doctrine\DBAL\Driver\PDOMySql\Driver) {
            if ($info[0] == 23000 && $info[1] == self::MYSQL_ERR_UNIQUE) {
                // unique fail
                $columns = [];
                try {
                    if (preg_match('~Duplicate entry .*? for key \'([^\']+)\'~', $info[2], $m) && ($table = self::resolveExceptionTable($e)) && ($indexes = $this->getSchemaManager()->listTableIndexes($table)) && isset($indexes[$m[1]])) {
                        $columns[$m[1]] = $indexes[$m[1]]->getColumns();
                    }
                } catch (\Exception $e) {
                }
                return new DuplicateEntryException($e, $columns, $query, $params, $this);
            } elseif ($info[0] == 23000 && $info[1] == self::MYSQL_ERR_NOT_NULL) {
                // notnull fail
                $column = NULL;
                if (preg_match('~Column \'([^\']+)\'~', $info[2], $m)) {
                    $column = $m[1];
                }
                return new EmptyValueException($e, $column, $query, $params, $this);
            }
        }
        $raw = $e;
        do {
            $raw = $raw->getPrevious();
        } while ($raw && !$raw instanceof \PDOException);
        return new DBALException($e, $query, $params, $this, $raw ? $raw->getMessage() : $e->getMessage());
    }

Usage Example

 /**
  * When entity have columns for required associations, this will fail.
  * Calls $em->flush().
  *
  * @todo fix error codes! PDO is returning database-specific codes
  *
  * @param object $entity
  * @throws \Doctrine\DBAL\DBALException
  * @throws \Exception
  * @return bool|object
  */
 public function persist($entity)
 {
     $this->db->beginTransaction();
     try {
         $persisted = $this->doInsert($entity);
         $this->db->commit();
         return $persisted;
     } catch (Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
         $this->db->rollback();
         return FALSE;
     } catch (Kdyby\Doctrine\DuplicateEntryException $e) {
         $this->db->rollback();
         return FALSE;
     } catch (DBALException $e) {
         $this->db->rollback();
         if ($this->isUniqueConstraintViolation($e)) {
             return FALSE;
         }
         throw $this->db->resolveException($e);
     } catch (\Exception $e) {
         $this->db->rollback();
         throw $e;
     } catch (\Throwable $e) {
         $this->db->rollback();
         throw $e;
     }
 }