Skip to content

Commit 81f7bc0

Browse files
committed
add method to easily check for duplicate key errs
1 parent ad9268d commit 81f7bc0

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

postgres/src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ impl Error {
3131
pub fn is_row_not_found(&self) -> bool {
3232
matches!(self.kind, ErrorKind::RowNotFound)
3333
}
34+
35+
/// Whether this is a duplicate key error (unique constraint violation).
36+
pub fn is_duplicate_key(&self) -> bool {
37+
if let ErrorKind::Postgres(err) = &self.kind {
38+
err.code() == Some(&tokio_postgres::error::SqlState::UNIQUE_VIOLATION)
39+
} else {
40+
false
41+
}
42+
}
3443
}
3544

3645
impl error::Error for Error {

0 commit comments

Comments
 (0)