PDOException.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Driver\PDO\Exception;
  4. use Doctrine\Deprecations\Deprecation;
  5. /**
  6. * @deprecated Use {@link Exception} instead
  7. *
  8. * @psalm-immutable
  9. */
  10. class PDOException extends \PDOException implements DriverException
  11. {
  12. /**
  13. * The driver specific error code.
  14. *
  15. * @var int|string|null
  16. */
  17. private $errorCode;
  18. /**
  19. * The SQLSTATE of the driver.
  20. *
  21. * @var string|null
  22. */
  23. private $sqlState;
  24. /**
  25. * @param \PDOException $exception The PDO exception to wrap.
  26. */
  27. public function __construct(\PDOException $exception)
  28. {
  29. parent::__construct($exception->getMessage(), 0, $exception);
  30. $this->code = $exception->getCode();
  31. $this->errorInfo = $exception->errorInfo;
  32. $this->errorCode = $exception->errorInfo[1] ?? $exception->getCode();
  33. $this->sqlState = $exception->errorInfo[0] ?? $exception->getCode();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getErrorCode()
  39. {
  40. /** @psalm-suppress ImpureMethodCall */
  41. Deprecation::triggerIfCalledFromOutside(
  42. 'doctrine/dbal',
  43. 'https://github.com/doctrine/dbal/pull/4112',
  44. 'Driver\AbstractException::getErrorCode() is deprecated, use getSQLState() or getCode() instead.'
  45. );
  46. return $this->errorCode;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getSQLState()
  52. {
  53. return $this->sqlState;
  54. }
  55. }