Skip to content

Commit a0c7769

Browse files
authored
Feature: Add exclusion violation error kind (#3918)
* feat: add exclusion violation error kind * chore: add test for exclusion error kind
1 parent 9de593a commit a0c7769

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

sqlx-core/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ pub enum ErrorKind {
197197
NotNullViolation,
198198
/// Check constraint violation.
199199
CheckViolation,
200+
/// Exclusion constraint violation.
201+
ExclusionViolation,
200202
/// An unmapped error.
201203
Other,
202204
}

sqlx-postgres/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl DatabaseError for PgDatabaseError {
214214
error_codes::FOREIGN_KEY_VIOLATION => ErrorKind::ForeignKeyViolation,
215215
error_codes::NOT_NULL_VIOLATION => ErrorKind::NotNullViolation,
216216
error_codes::CHECK_VIOLATION => ErrorKind::CheckViolation,
217+
error_codes::EXCLUSION_VIOLATION => ErrorKind::ExclusionViolation,
217218
_ => ErrorKind::Other,
218219
}
219220
}
@@ -239,4 +240,6 @@ pub(crate) mod error_codes {
239240
pub const NOT_NULL_VIOLATION: &str = "23502";
240241
/// Caused when a check constraint is violated.
241242
pub const CHECK_VIOLATION: &str = "23514";
243+
/// Caused when a exclude constraint is violated.
244+
pub const EXCLUSION_VIOLATION: &str = "23P01";
242245
}

tests/postgres/error.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ async fn it_fails_with_check_violation() -> anyhow::Result<()> {
7575
Ok(())
7676
}
7777

78+
#[sqlx_macros::test]
79+
async fn it_fails_with_exclude_violation() -> anyhow::Result<()> {
80+
let mut conn = new::<Postgres>().await?;
81+
let mut tx = conn.begin().await?;
82+
83+
sqlx::query("INSERT INTO circles VALUES (circle('(0,0)'::point, 5.0));")
84+
.execute(&mut *tx)
85+
.await?;
86+
87+
let res: Result<_, sqlx::Error> =
88+
sqlx::query("INSERT INTO circles VALUES (circle('(0,2.0)'::point, 2.0));")
89+
.execute(&mut *tx)
90+
.await;
91+
let err = res.unwrap_err();
92+
93+
let err = err.into_database_error().unwrap();
94+
95+
assert_eq!(err.kind(), ErrorKind::ExclusionViolation);
96+
97+
Ok(())
98+
}
99+
78100
#[sqlx_macros::test]
79101
async fn it_fails_with_begin_failed() -> anyhow::Result<()> {
80102
let mut conn = new::<Postgres>().await?;

tests/postgres/setup.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,8 @@ CREATE SCHEMA IF NOT EXISTS foo;
6363
CREATE TYPE foo."Foo" as ENUM ('Bar', 'Baz');
6464

6565
CREATE TABLE mytable(f HSTORE);
66+
67+
CREATE TABLE circles (
68+
c circle,
69+
EXCLUDE USING gist (c WITH &&)
70+
);

0 commit comments

Comments
 (0)