PDOException.php 1.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

namespace Doctrine\DBAL\Driver;

/**
 * Tiny wrapper for PDOException instances to implement the {@link DriverException} interface.
 */
class PDOException extends \PDOException implements DriverException
{
    /**
     * The driver specific error code.
     *
13
     * @var int|string|null
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
     */
    private $errorCode;

    /**
     * The SQLSTATE of the driver.
     *
     * @var string|null
     */
    private $sqlState;

    /**
     * @param \PDOException $exception The PDO exception to wrap.
     */
    public function __construct(\PDOException $exception)
    {
29
        parent::__construct($exception->getMessage(), 0, $exception);
30

31 32
        $this->code      = $exception->getCode();
        $this->errorInfo = $exception->errorInfo;
33 34
        $this->errorCode = $exception->errorInfo[1] ?? $exception->getCode();
        $this->sqlState  = $exception->errorInfo[0] ?? $exception->getCode();
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    }

    /**
     * {@inheritdoc}
     */
    public function getErrorCode()
    {
        return $this->errorCode;
    }

    /**
     * {@inheritdoc}
     */
    public function getSQLState()
    {
        return $this->sqlState;
    }
}