$params */ public function __construct( string $message, private readonly string $sql = '', private readonly array $params = [], private readonly string $context = '', ?\Throwable $previous = null, ) { parent::__construct($message, 0, $previous); } /** * Maps a PDOException to the narrowest exception type the SQLSTATE allows. * * @param list $params */ public static function fromPdoException( PDOException $exception, string $context, string $sql = '', array $params = [], ): self { $sqlState = (string) $exception->getCode(); $driverCode = (int) ($exception->errorInfo[1] ?? 0); $message = sprintf('Database error during %s: %s', $context, $exception->getMessage()); if ($sqlState === '23000' && in_array($driverCode, [1062, 1555, 1586], true)) { return new DuplicateKeyException($message, $sql, $params, $context, $exception); } if ($sqlState === '23000' && in_array($driverCode, [1451, 1452], true)) { return new ForeignKeyViolationException($message, $sql, $params, $context, $exception); } if ($sqlState === 'HY000' && in_array($driverCode, [1205, 1213], true)) { return new LockTimeoutException($message, $sql, $params, $context, $exception); } return new self($message, $sql, $params, $context, $exception); } public function sql(): string { return $this->sql; } /** @return list */ public function params(): array { return $this->params; } public function context(): string { return $this->context; } }