| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace Doctrine\DBAL\Driver\API\PostgreSQL;
|
|
|
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\DatabaseDoesNotExist;
|
|
|
11 |
use Doctrine\DBAL\Exception\DeadlockException;
|
|
|
12 |
use Doctrine\DBAL\Exception\DriverException;
|
|
|
13 |
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
|
|
|
14 |
use Doctrine\DBAL\Exception\InvalidFieldNameException;
|
|
|
15 |
use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
|
|
|
16 |
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
|
|
|
17 |
use Doctrine\DBAL\Exception\SchemaDoesNotExist;
|
|
|
18 |
use Doctrine\DBAL\Exception\SyntaxErrorException;
|
|
|
19 |
use Doctrine\DBAL\Exception\TableExistsException;
|
|
|
20 |
use Doctrine\DBAL\Exception\TableNotFoundException;
|
|
|
21 |
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
|
|
22 |
use Doctrine\DBAL\Query;
|
|
|
23 |
|
|
|
24 |
use function strpos;
|
|
|
25 |
|
|
|
26 |
/** @internal */
|
|
|
27 |
final class ExceptionConverter implements ExceptionConverterInterface
|
|
|
28 |
{
|
|
|
29 |
/** @link http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html */
|
|
|
30 |
public function convert(Exception $exception, ?Query $query): DriverException
|
|
|
31 |
{
|
|
|
32 |
switch ($exception->getSQLState()) {
|
|
|
33 |
case '40001':
|
|
|
34 |
case '40P01':
|
|
|
35 |
return new DeadlockException($exception, $query);
|
|
|
36 |
|
|
|
37 |
case '0A000':
|
|
|
38 |
// Foreign key constraint violations during a TRUNCATE operation
|
|
|
39 |
// are considered "feature not supported" in PostgreSQL.
|
|
|
40 |
if (strpos($exception->getMessage(), 'truncate') !== false) {
|
|
|
41 |
return new ForeignKeyConstraintViolationException($exception, $query);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
break;
|
|
|
45 |
|
|
|
46 |
case '23502':
|
|
|
47 |
return new NotNullConstraintViolationException($exception, $query);
|
|
|
48 |
|
|
|
49 |
case '23503':
|
|
|
50 |
return new ForeignKeyConstraintViolationException($exception, $query);
|
|
|
51 |
|
|
|
52 |
case '23505':
|
|
|
53 |
return new UniqueConstraintViolationException($exception, $query);
|
|
|
54 |
|
|
|
55 |
case '3D000':
|
|
|
56 |
return new DatabaseDoesNotExist($exception, $query);
|
|
|
57 |
|
|
|
58 |
case '3F000':
|
|
|
59 |
return new SchemaDoesNotExist($exception, $query);
|
|
|
60 |
|
|
|
61 |
case '42601':
|
|
|
62 |
return new SyntaxErrorException($exception, $query);
|
|
|
63 |
|
|
|
64 |
case '42702':
|
|
|
65 |
return new NonUniqueFieldNameException($exception, $query);
|
|
|
66 |
|
|
|
67 |
case '42703':
|
|
|
68 |
return new InvalidFieldNameException($exception, $query);
|
|
|
69 |
|
|
|
70 |
case '42P01':
|
|
|
71 |
return new TableNotFoundException($exception, $query);
|
|
|
72 |
|
|
|
73 |
case '42P07':
|
|
|
74 |
return new TableExistsException($exception, $query);
|
|
|
75 |
|
|
|
76 |
case '08006':
|
|
|
77 |
return new ConnectionException($exception, $query);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// Prior to fixing https://bugs.php.net/bug.php?id=64705 (PHP 7.4.10),
|
|
|
81 |
// in some cases (mainly connection errors) the PDO exception wouldn't provide a SQLSTATE via its code.
|
|
|
82 |
// We have to match against the SQLSTATE in the error message in these cases.
|
|
|
83 |
if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) {
|
|
|
84 |
return new ConnectionException($exception, $query);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return new DriverException($exception, $query);
|
|
|
88 |
}
|
|
|
89 |
}
|