Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added enum for SQLx Error and basic functions for it #1707

Merged
merged 26 commits into from Jun 19, 2023
Merged
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
01d6050
Added enum for SQL Error and basic functions for it
darkmmon Jun 13, 2023
e973517
fmt
darkmmon Jun 13, 2023
6de76f9
Restructured code to allow multiple database system simultaneously
darkmmon Jun 14, 2023
fd337e1
updated code error and moved Eq to derive by compiler
darkmmon Jun 14, 2023
268c6f2
added ForeignKeyViolation implementation
darkmmon Jun 14, 2023
2230575
fmt
darkmmon Jun 14, 2023
015172c
added type param
darkmmon Jun 14, 2023
96201b8
changed type param and used unwrap_or_default instead of unwrap
darkmmon Jun 14, 2023
1d84145
fmt
darkmmon Jun 14, 2023
f19f28b
refactor code and update documentation
darkmmon Jun 14, 2023
90c109f
updated some error code and fixed formatting
darkmmon Jun 14, 2023
64b5176
create SQL Err test for UniqueConstraintViolation
darkmmon Jun 14, 2023
610a470
fmt
darkmmon Jun 14, 2023
bdd432f
updated error codes for mysql errors
darkmmon Jun 15, 2023
b693813
added test for ForeignKeyConstraintViolation and removed unused impor…
darkmmon Jun 15, 2023
acc9ed4
fmt
darkmmon Jun 15, 2023
96e566e
updated doc and error message
darkmmon Jun 15, 2023
df52415
added static str in SqlErr
darkmmon Jun 16, 2023
02dceff
changed to add error message into SqlErr as String
darkmmon Jun 16, 2023
5fb5128
updated test file to check database type through connection
darkmmon Jun 16, 2023
90279ae
fmt
darkmmon Jun 16, 2023
6fe8c5b
updated test for SqlErr to match the error type only
darkmmon Jun 19, 2023
ae8067f
Removed comment and fixed grammar mistake
darkmmon Jun 19, 2023
144976d
fmt
darkmmon Jun 19, 2023
da0660b
Update src/error.rs
tyt2y3 Jun 19, 2023
156e577
Refactoring
billy1624 Jun 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/error.rs
Expand Up @@ -139,3 +139,65 @@ where
{
DbErr::Json(s.to_string())
}

/// An error from unsuccessful SQL query
#[derive(Error, Debug, PartialEq, Eq)]
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
#[non_exhaustive]
pub enum SqlErr {
/// error for inserting a record with a key that already exists in the table
#[error("Cannot have record with same key")]
UniqueConstraintViolation(),
/// error for Foreign key is not primary key
#[error("Cannot add non-primary key from other table")]
ForeignKeyConstraintViolation(),
darkmmon marked this conversation as resolved.
Show resolved Hide resolved
}

#[allow(dead_code)]
impl DbErr {
/// converting generic DbErr from mysql to SqlErr
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-postgres",
feature = "sqlx-sqlite"
))]
fn sql_err<E: sqlx::error::DatabaseError>(&self) -> Option<SqlErr> {
if let DbErr::Exec(RuntimeErr::SqlxError(sqlx::Error::Database(e)))
| DbErr::Query(RuntimeErr::SqlxError(sqlx::Error::Database(e))) = self
{
#[cfg(feature = "sqlx-mysql")]
{
if e.try_downcast_ref::<SqlxMySqlError>().is_some() {
if e.code().unwrap().eq("1062") | e.code().unwrap().eq("1586") {
return Some(SqlErr::UniqueConstraintViolation());
};
if e.code().unwrap().eq("1452") {
return Some(SqlErr::ForeignKeyConstraintViolation());
};
}
}
billy1624 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "sqlx-postgres")]
{
if e.try_downcast_ref::<SqlxPostgresError>().is_some() {
if e.code().unwrap().eq("23505") {
return Some(SqlErr::UniqueConstraintViolation());
};
if e.code().unwrap().eq("23503") {
return Some(SqlErr::ForeignKeyConstraintViolation());
};
}
}
#[cfg(feature = "sqlx-sqlite")]
{
if e.try_downcast_ref::<SqlxSqliteError>().is_some() {
if e.code().unwrap().eq("2067") {
return Some(SqlErr::UniqueConstraintViolation());
};
if e.code().unwrap().eq("787") {
return Some(SqlErr::ForeignKeyConstraintViolation());
};
}
}
}
None
}
}