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

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