summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Exception/DriverException.php
blob: d794d7768db890cb446a66f1c455b2b6fe1909b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Exception;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Query;

use function assert;

/**
 * Base class for all errors detected in the driver.
 *
 * @psalm-immutable
 */
class DriverException extends \Exception implements Exception, Driver\Exception
{
    /**
     * @internal
     *
     * @param Driver\Exception $driverException The DBAL driver exception to chain.
     * @param Query|null       $query           The SQL query that triggered the exception, if any.
     */
    public function __construct(
        Driver\Exception $driverException,
        private readonly ?Query $query,
    ) {
        if ($query !== null) {
            $message = 'An exception occurred while executing a query: ' . $driverException->getMessage();
        } else {
            $message = 'An exception occurred in the driver: ' . $driverException->getMessage();
        }

        parent::__construct($message, $driverException->getCode(), $driverException);
    }

    public function getSQLState(): ?string
    {
        $previous = $this->getPrevious();
        assert($previous instanceof Driver\Exception);

        return $previous->getSQLState();
    }

    public function getQuery(): ?Query
    {
        return $this->query;
    }
}