vendor/doctrine/dbal/src/Driver/API/PostgreSQL/ExceptionConverter.php line 87

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