PDOException.php 1.14 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\DBAL\Driver;

/**
 * Tiny wrapper for PDOException instances to implement the {@link DriverException} interface.
7 8
 *
 * @psalm-immutable
9 10 11 12 13 14
 */
class PDOException extends \PDOException implements DriverException
{
    /**
     * The driver specific error code.
     *
15
     * @var int|string|null
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
     */
    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)
    {
31
        parent::__construct($exception->getMessage(), 0, $exception);
32

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

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

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