|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Crate\DBAL\Driver\PDOCrate; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface; |
|
8
|
|
|
use Doctrine\DBAL\Driver\Exception; |
|
|
|
|
|
|
9
|
|
|
use Doctrine\DBAL\Exception\ConnectionException; |
|
10
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
|
11
|
|
|
use Doctrine\DBAL\Exception\InvalidFieldNameException; |
|
12
|
|
|
use Doctrine\DBAL\Exception\SchemaDoesNotExist; |
|
13
|
|
|
use Doctrine\DBAL\Exception\SyntaxErrorException; |
|
14
|
|
|
use Doctrine\DBAL\Exception\TableExistsException; |
|
15
|
|
|
use Doctrine\DBAL\Exception\TableNotFoundException; |
|
16
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
17
|
|
|
use Doctrine\DBAL\Query; |
|
18
|
|
|
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist; |
|
19
|
|
|
|
|
20
|
|
|
use function strpos; |
|
21
|
|
|
|
|
22
|
|
|
/** @internal */ |
|
23
|
|
|
final class ExceptionConverter implements ExceptionConverterInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @link https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-codes */ |
|
26
|
|
|
public function convert(Exception $exception, ?Query $query): DriverException |
|
27
|
|
|
{ |
|
28
|
|
|
switch ($exception->getCode()) { |
|
29
|
|
|
case '4000': |
|
30
|
|
|
return new SyntaxErrorException($exception, $query); |
|
31
|
|
|
|
|
32
|
|
|
case '4008': |
|
33
|
|
|
case '4043': |
|
34
|
|
|
return new InvalidFieldNameException($exception, $query); |
|
35
|
|
|
|
|
36
|
|
|
case '4041': |
|
37
|
|
|
return new TableNotFoundException($exception, $query); |
|
38
|
|
|
|
|
39
|
|
|
case '4045': |
|
40
|
|
|
return new SchemaDoesNotExist($exception, $query); |
|
41
|
|
|
|
|
42
|
|
|
case '4091': |
|
43
|
|
|
return new UniqueConstraintViolationException($exception, $query); |
|
44
|
|
|
|
|
45
|
|
|
case '4093': |
|
46
|
|
|
return new TableExistsException($exception, $query); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Prior to fixing https://bugs.php.net/bug.php?id=64705 (PHP 7.4.10), |
|
50
|
|
|
// in some cases (mainly connection errors) the PDO exception wouldn't provide a SQLSTATE via its code. |
|
51
|
|
|
// We have to match against the SQLSTATE in the error message in these cases. |
|
52
|
|
|
if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { |
|
53
|
|
|
return new ConnectionException($exception, $query); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return new DriverException($exception, $query); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: