Switched from diesel to sqlx
This commit is contained in:
46
src/error.rs
46
src/error.rs
@@ -1,5 +1,4 @@
|
||||
use std::fmt;
|
||||
use diesel::result::Error as DieselError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub type SirenResult<T> = Result<T, Error>;
|
||||
@@ -37,22 +36,47 @@ impl From<std::string::FromUtf8Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DieselError> for Error {
|
||||
fn from(error: DieselError) -> Self {
|
||||
impl From<sqlx::Error> for Error {
|
||||
fn from(error: sqlx::Error) -> Self {
|
||||
match error {
|
||||
DieselError::DatabaseError(kind, err) => match kind {
|
||||
diesel::result::DatabaseErrorKind::UniqueViolation => {
|
||||
Self::new(409, err.message().to_string())
|
||||
sqlx::Error::RowNotFound => Error::new(404, "Not found".to_string()),
|
||||
sqlx::Error::ColumnIndexOutOfBounds { .. } => Error::new(422, error.to_string()),
|
||||
sqlx::Error::ColumnNotFound { .. } => Error::new(422, error.to_string()),
|
||||
sqlx::Error::ColumnDecode { .. } => Error::new(422, error.to_string()),
|
||||
sqlx::Error::Decode(_) => Error::new(422, error.to_string()),
|
||||
sqlx::Error::PoolTimedOut => Error::new(503, error.to_string()),
|
||||
sqlx::Error::PoolClosed => Error::new(503, error.to_string()),
|
||||
sqlx::Error::Tls(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Io(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Protocol(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Configuration(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::AnyDriverError(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Database(err) => {
|
||||
if let Some(code) = err.code() {
|
||||
match code.trim() {
|
||||
// Unique violation
|
||||
"23505" => return Error::new(409, err.to_string()),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
_ => Self::new(500, err.message().to_string()),
|
||||
},
|
||||
DieselError::NotFound => Self::new(404, "The record was not found".to_string()),
|
||||
DieselError::SerializationError(err) => Self::new(422, err.to_string()),
|
||||
err => Self::new(500, format!("Unknown database error: {}", err)),
|
||||
Error::new(500, err.to_string())
|
||||
}
|
||||
sqlx::Error::Migrate(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::TypeNotFound { type_name } => {
|
||||
Error::new(500, format!("Type not found: {}", type_name))
|
||||
}
|
||||
sqlx::Error::WorkerCrashed => Error::new(500, error.to_string()),
|
||||
_ => Error::new(500, error.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::migrate::MigrateError> for Error {
|
||||
fn from(error: sqlx::migrate::MigrateError) -> Self {
|
||||
Error::new(500, error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(error: reqwest::Error) -> Self {
|
||||
Self::new(500, format!("Unknown reqwest error: {}", error))
|
||||
|
||||
Reference in New Issue
Block a user