Files
packager/rust/src/models/error.rs

159 lines
4.7 KiB
Rust
Raw Normal View History

2023-08-29 21:34:00 +02:00
use std::fmt;
use sqlx::error::DatabaseError as _;
pub enum DatabaseError {
/// Errors we can receive **from** the database that are caused by connection
/// problems or schema problems (e.g. we get a return value that does not fit our enum,
/// or a wrongly formatted date)
Sql {
description: String,
},
Uuid {
description: String,
},
Enum {
description: String,
},
TimeParse {
description: String,
},
}
impl fmt::Display for DatabaseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Sql { description } => {
write!(f, "SQL error: {description}")
}
Self::Uuid { description } => {
write!(f, "UUID error: {description}")
}
Self::Enum { description } => {
write!(f, "Enum error: {description}")
}
Self::TimeParse { description } => {
write!(f, "Date parse error: {description}")
}
}
}
}
pub enum QueryError {
/// Errors that are caused by wrong input data, e.g. ids that cannot be found, or
/// inserts that violate unique constraints
Duplicate {
description: String,
},
NotFound {
description: String,
},
ReferenceNotFound {
description: String,
},
}
impl fmt::Display for QueryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Duplicate { description } => {
write!(f, "Duplicate data entry: {description}")
}
Self::NotFound { description } => {
write!(f, "not found: {description}")
}
Self::ReferenceNotFound { description } => {
write!(f, "SQL foreign key reference was not found: {description}")
}
}
}
}
pub enum Error {
Database(DatabaseError),
Query(QueryError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Database(error) => write!(f, "{}", error),
Self::Query(error) => write!(f, "{}", error),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// defer to Display
write!(f, "SQL error: {self}")
}
}
2023-08-29 21:34:00 +02:00
impl From<uuid::Error> for Error {
2023-08-29 21:34:00 +02:00
fn from(value: uuid::Error) -> Self {
Error::Database(DatabaseError::Uuid {
description: value.to_string(),
})
}
}
2023-08-29 21:34:00 +02:00
impl From<time::error::Format> for Error {
2023-08-29 21:34:00 +02:00
fn from(value: time::error::Format) -> Self {
Error::Database(DatabaseError::TimeParse {
description: value.to_string(),
})
}
}
2023-08-29 21:34:00 +02:00
impl From<sqlx::Error> for Error {
2023-08-29 21:34:00 +02:00
fn from(value: sqlx::Error) -> Self {
match value {
sqlx::Error::RowNotFound => Error::Query(QueryError::NotFound {
description: value.to_string(),
}),
sqlx::Error::Database(ref error) => {
let sqlite_error = error.downcast_ref::<sqlx::sqlite::SqliteError>();
if let Some(code) = sqlite_error.code() {
match &*code {
// SQLITE_CONSTRAINT_FOREIGNKEY
2023-08-29 21:34:00 +02:00
"787" => Error::Query(QueryError::ReferenceNotFound {
2023-08-29 21:34:00 +02:00
description: "foreign key reference not found".to_string(),
2023-08-29 21:34:00 +02:00
}),
// SQLITE_CONSTRAINT_UNIQUE
2023-08-29 21:34:00 +02:00
"2067" => Error::Query(QueryError::Duplicate {
2023-08-29 21:34:00 +02:00
description: "item with unique constraint already exists".to_string(),
2023-08-29 21:34:00 +02:00
}),
_ => Error::Database(DatabaseError::Sql {
description: format!(
"got error with unknown code: {}",
2023-08-29 21:34:00 +02:00
sqlite_error
2023-08-29 21:34:00 +02:00
),
}),
}
} else {
Error::Database(DatabaseError::Sql {
description: format!(
"got error without code: {}",
2023-08-29 21:34:00 +02:00
sqlite_error
2023-08-29 21:34:00 +02:00
),
})
}
}
_ => Error::Database(DatabaseError::Sql {
2023-08-29 21:34:00 +02:00
description: format!("got unknown error: {}", value),
2023-08-29 21:34:00 +02:00
}),
}
}
}
2023-08-29 21:34:00 +02:00
impl From<time::error::Parse> for Error {
2023-08-29 21:34:00 +02:00
fn from(value: time::error::Parse) -> Self {
Error::Database(DatabaseError::TimeParse {
description: value.to_string(),
})
}
}
impl std::error::Error for Error {}