AbstractException.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\DBAL\Driver;
  4. use Doctrine\Deprecations\Deprecation;
  5. use Exception as BaseException;
  6. use Throwable;
  7. /**
  8. * Base implementation of the {@link Exception} interface.
  9. *
  10. * @internal
  11. *
  12. * @psalm-immutable
  13. */
  14. abstract class AbstractException extends BaseException implements DriverException
  15. {
  16. /**
  17. * The driver specific error code.
  18. *
  19. * @var int|string|null
  20. */
  21. private $errorCode;
  22. /**
  23. * The SQLSTATE of the driver.
  24. *
  25. * @var string|null
  26. */
  27. private $sqlState;
  28. /**
  29. * @param string $message The driver error message.
  30. * @param string|null $sqlState The SQLSTATE the driver is in at the time the error occurred, if any.
  31. * @param int|string|null $errorCode The driver specific error code if any.
  32. */
  33. public function __construct($message, $sqlState = null, $errorCode = null, ?Throwable $previous = null)
  34. {
  35. parent::__construct($message, 0, $previous);
  36. $this->errorCode = $errorCode;
  37. $this->sqlState = $sqlState;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getErrorCode()
  43. {
  44. /** @psalm-suppress ImpureMethodCall */
  45. Deprecation::triggerIfCalledFromOutside(
  46. 'doctrine/dbal',
  47. 'https://github.com/doctrine/dbal/pull/4112',
  48. 'Driver\AbstractException::getErrorCode() is deprecated, use getSQLState() or getCode() instead.'
  49. );
  50. return $this->errorCode;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getSQLState()
  56. {
  57. return $this->sqlState;
  58. }
  59. }