Skip to content

Commit 1918c12

Browse files
committed
Postgres Error Display
1 parent ae37e59 commit 1918c12

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

core/src/database/postgres/client.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,23 @@ pub enum PostgresConnectionError {
4747

4848
#[derive(thiserror::Error, Debug)]
4949
pub enum PostgresError {
50-
#[error("PgError {0}")]
51-
PgError(#[from] PgError),
50+
#[error("Postgres database error: {0}")]
51+
PgError(String),
5252

5353
#[error("Connection pool error: {0}")]
5454
ConnectionPoolError(#[from] RunError<tokio_postgres::Error>),
5555
}
5656

57+
impl From<tokio_postgres::error::Error> for PostgresError {
58+
fn from(err: PgError) -> Self {
59+
if let Some(db_err) = err.as_db_error() {
60+
PostgresError::PgError(db_err.message().to_string())
61+
} else {
62+
PostgresError::PgError(err.to_string())
63+
}
64+
}
65+
}
66+
5767
#[allow(unused)]
5868
pub struct PostgresTransaction<'a> {
5969
pub transaction: PgTransaction<'a>,
@@ -66,17 +76,17 @@ impl PostgresTransaction<'_> {
6676
query: &str,
6777
params: &[&(dyn ToSql + Sync)],
6878
) -> Result<u64, PostgresError> {
69-
self.transaction.execute(query, params).await.map_err(PostgresError::PgError)
79+
self.transaction.execute(query, params).await.map_err(PostgresError::from)
7080
}
7181

7282
#[allow(unused)]
7383
pub async fn commit(self) -> Result<(), PostgresError> {
74-
self.transaction.commit().await.map_err(PostgresError::PgError)
84+
self.transaction.commit().await.map_err(PostgresError::from)
7585
}
7686

7787
#[allow(unused)]
7888
pub async fn rollback(self) -> Result<(), PostgresError> {
79-
self.transaction.rollback().await.map_err(PostgresError::PgError)
89+
self.transaction.rollback().await.map_err(PostgresError::from)
8090
}
8191
}
8292

@@ -170,7 +180,7 @@ impl PostgresClient {
170180

171181
pub async fn batch_execute(&self, sql: &str) -> Result<(), PostgresError> {
172182
let conn = self.pool.get().await?;
173-
conn.batch_execute(sql).await.map_err(PostgresError::PgError)
183+
conn.batch_execute(sql).await.map_err(PostgresError::from)
174184
}
175185

176186
pub async fn execute<T>(
@@ -182,7 +192,7 @@ impl PostgresClient {
182192
T: ?Sized + ToStatement,
183193
{
184194
let conn = self.pool.get().await?;
185-
conn.execute(query, params).await.map_err(PostgresError::PgError)
195+
conn.execute(query, params).await.map_err(PostgresError::from)
186196
}
187197

188198
pub async fn prepare(
@@ -191,7 +201,7 @@ impl PostgresClient {
191201
parameter_types: &[PgType],
192202
) -> Result<Statement, PostgresError> {
193203
let conn = self.pool.get().await?;
194-
conn.prepare_typed(query, parameter_types).await.map_err(PostgresError::PgError)
204+
conn.prepare_typed(query, parameter_types).await.map_err(PostgresError::from)
195205
}
196206

197207
pub async fn with_transaction<F, Fut, T, Q>(
@@ -206,13 +216,13 @@ impl PostgresClient {
206216
Q: ?Sized + ToStatement,
207217
{
208218
let mut conn = self.pool.get().await.map_err(PostgresError::ConnectionPoolError)?;
209-
let transaction = conn.transaction().await.map_err(PostgresError::PgError)?;
219+
let transaction = conn.transaction().await.map_err(PostgresError::from)?;
210220

211-
let count = transaction.execute(query, params).await.map_err(PostgresError::PgError)?;
221+
let count = transaction.execute(query, params).await.map_err(PostgresError::from)?;
212222

213223
let result = f(count).await?;
214224

215-
transaction.commit().await.map_err(PostgresError::PgError)?;
225+
transaction.commit().await.map_err(PostgresError::from)?;
216226

217227
Ok(result)
218228
}
@@ -226,7 +236,7 @@ impl PostgresClient {
226236
T: ?Sized + ToStatement,
227237
{
228238
let conn = self.pool.get().await?;
229-
let rows = conn.query(query, params).await.map_err(PostgresError::PgError)?;
239+
let rows = conn.query(query, params).await.map_err(PostgresError::from)?;
230240
Ok(rows)
231241
}
232242

@@ -239,7 +249,7 @@ impl PostgresClient {
239249
T: ?Sized + ToStatement,
240250
{
241251
let conn = self.pool.get().await?;
242-
let row = conn.query_one(query, params).await.map_err(PostgresError::PgError)?;
252+
let row = conn.query_one(query, params).await.map_err(PostgresError::from)?;
243253
Ok(row)
244254
}
245255

@@ -252,7 +262,7 @@ impl PostgresClient {
252262
T: ?Sized + ToStatement,
253263
{
254264
let conn = self.pool.get().await?;
255-
let row = conn.query_opt(query, params).await.map_err(PostgresError::PgError)?;
265+
let row = conn.query_opt(query, params).await.map_err(PostgresError::from)?;
256266
Ok(row)
257267
}
258268

@@ -265,15 +275,15 @@ impl PostgresClient {
265275
T: ?Sized + ToStatement,
266276
{
267277
let mut conn = self.pool.get().await?;
268-
let transaction = conn.transaction().await.map_err(PostgresError::PgError)?;
278+
let transaction = conn.transaction().await.map_err(PostgresError::from)?;
269279

270280
for params in params_list {
271281
let params_refs: Vec<&(dyn ToSql + Sync)> =
272282
params.iter().map(|param| param.as_ref() as &(dyn ToSql + Sync)).collect();
273-
transaction.execute(query, &params_refs).await.map_err(PostgresError::PgError)?;
283+
transaction.execute(query, &params_refs).await.map_err(PostgresError::from)?;
274284
}
275285

276-
transaction.commit().await.map_err(PostgresError::PgError)?;
286+
transaction.commit().await.map_err(PostgresError::from)?;
277287
Ok(())
278288
}
279289

@@ -284,7 +294,7 @@ impl PostgresClient {
284294
{
285295
let conn = self.pool.get().await?;
286296

287-
conn.copy_in(statement).await.map_err(PostgresError::PgError)
297+
conn.copy_in(statement).await.map_err(PostgresError::from)
288298
}
289299

290300
// Internal method used by insert_bulk for large datasets (>100 rows).

documentation/docs/pages/docs/changelog.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
-------------------------------------------------
1111
- fix: reorg safe range calculation for out-of-range an error message
1212
- fix: Upgrade dependecy minor versions to get latest alloy fixes
13+
- fix: Improve error dislay for pg error
1314

1415
### Breaking changes
1516
-------------------------------------------------

0 commit comments

Comments
 (0)