more error refactoring

This commit is contained in:
2023-08-29 21:34:00 +02:00
parent ccb666f000
commit 3a50f3e9f0
7 changed files with 348 additions and 494 deletions

View File

@@ -1,4 +1,3 @@
use std::convert;
use std::fmt;
use sqlx::error::DatabaseError as _;
@@ -43,9 +42,6 @@ impl fmt::Display for DatabaseError {
pub enum QueryError {
/// Errors that are caused by wrong input data, e.g. ids that cannot be found, or
/// inserts that violate unique constraints
Constraint {
description: String,
},
Duplicate {
description: String,
},
@@ -60,9 +56,6 @@ pub enum QueryError {
impl fmt::Display for QueryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Constraint { description } => {
write!(f, "SQL constraint error: {description}")
}
Self::Duplicate { description } => {
write!(f, "Duplicate data entry: {description}")
}
@@ -97,7 +90,7 @@ impl fmt::Debug for Error {
}
}
impl convert::From<uuid::Error> for Error {
impl From<uuid::Error> for Error {
fn from(value: uuid::Error) -> Self {
Error::Database(DatabaseError::Uuid {
description: value.to_string(),
@@ -105,7 +98,7 @@ impl convert::From<uuid::Error> for Error {
}
}
impl convert::From<time::error::Format> for Error {
impl From<time::error::Format> for Error {
fn from(value: time::error::Format) -> Self {
Error::Database(DatabaseError::TimeParse {
description: value.to_string(),
@@ -113,7 +106,7 @@ impl convert::From<time::error::Format> for Error {
}
}
impl convert::From<sqlx::Error> for Error {
impl From<sqlx::Error> for Error {
fn from(value: sqlx::Error) -> Self {
match value {
sqlx::Error::RowNotFound => Error::Query(QueryError::NotFound {
@@ -124,11 +117,11 @@ impl convert::From<sqlx::Error> for Error {
if let Some(code) = sqlite_error.code() {
match &*code {
// SQLITE_CONSTRAINT_FOREIGNKEY
"787" => Error::Query(QueryError::Constraint {
"787" => Error::Query(QueryError::ReferenceNotFound {
description: format!("foreign key reference not found"),
}),
// SQLITE_CONSTRAINT_UNIQUE
"2067" => Error::Query(QueryError::Constraint {
"2067" => Error::Query(QueryError::Duplicate {
description: format!("item with unique constraint already exists",),
}),
_ => Error::Database(DatabaseError::Sql {
@@ -154,7 +147,7 @@ impl convert::From<sqlx::Error> for Error {
}
}
impl convert::From<time::error::Parse> for Error {
impl From<time::error::Parse> for Error {
fn from(value: time::error::Parse) -> Self {
Error::Database(DatabaseError::TimeParse {
description: value.to_string(),