From c30e603ceaceecfbc55ca3a751a06ef71bbfdd09 Mon Sep 17 00:00:00 2001 From: bbldn05 Date: Sat, 29 Mar 2025 15:59:26 +0300 Subject: [PATCH] feat: Added database reference to exception message (for SQLite) --- src/Config/SQLite/ConnectionConfig.php | 5 +++++ src/Config/SQLite/FileConnectionConfig.php | 5 +++++ src/Config/SQLite/MemoryConnectionConfig.php | 5 +++++ src/Driver/SQLite/SQLiteDriver.php | 7 ++++++- src/Exception/StatementException.php | 7 +++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Config/SQLite/ConnectionConfig.php b/src/Config/SQLite/ConnectionConfig.php index 0aed250b..542227f6 100644 --- a/src/Config/SQLite/ConnectionConfig.php +++ b/src/Config/SQLite/ConnectionConfig.php @@ -25,4 +25,9 @@ public function getName(): string { return 'sqlite'; } + + public function formatExceptionMessage(string $message): string + { + return $message; + } } diff --git a/src/Config/SQLite/FileConnectionConfig.php b/src/Config/SQLite/FileConnectionConfig.php index ee1defb6..0fbaf7e3 100644 --- a/src/Config/SQLite/FileConnectionConfig.php +++ b/src/Config/SQLite/FileConnectionConfig.php @@ -44,4 +44,9 @@ public function getSourceString(): string { return $this->database; } + + public function formatExceptionMessage(string $message): string + { + return \sprintf('(%s) %s', $this->database, $message); + } } diff --git a/src/Config/SQLite/MemoryConnectionConfig.php b/src/Config/SQLite/MemoryConnectionConfig.php index 3c9d8c8a..ebf8e5b0 100644 --- a/src/Config/SQLite/MemoryConnectionConfig.php +++ b/src/Config/SQLite/MemoryConnectionConfig.php @@ -22,4 +22,9 @@ public function __construct(array $options = []) { parent::__construct(self::DATABASE_NAME, $options); } + + public function formatExceptionMessage(string $message): string + { + return $message; + } } diff --git a/src/Driver/SQLite/SQLiteDriver.php b/src/Driver/SQLite/SQLiteDriver.php index 9e40c180..12316205 100644 --- a/src/Driver/SQLite/SQLiteDriver.php +++ b/src/Driver/SQLite/SQLiteDriver.php @@ -21,6 +21,9 @@ use Cycle\Database\Query\InsertQuery; use Cycle\Database\Query\QueryBuilder; +/** + * @property SQLiteDriverConfig $config + */ class SQLiteDriver extends Driver { /** @@ -52,7 +55,9 @@ protected function mapException(\Throwable $exception, string $query): Statement return new StatementException\ConstrainException($exception, $query); } - return new StatementException($exception, $query); + return (new StatementException($exception, $query))->setMessage( + message: $this->config->connection->formatExceptionMessage($exception->getMessage()), + ); } protected function setIsolationLevel(string $level): void diff --git a/src/Exception/StatementException.php b/src/Exception/StatementException.php index 979627ac..4cb4c7f0 100644 --- a/src/Exception/StatementException.php +++ b/src/Exception/StatementException.php @@ -29,4 +29,11 @@ public function getQuery(): string { return $this->query; } + + public function setMessage(string $message): self + { + $this->message = $message; + + return $this; + } }