Skip to content

Commit

Permalink
Refactor Type Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Aug 28, 2022
1 parent fe6c40d commit 0b754ea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion issues/324/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro_rules! impl_try_from_u64_err {
($newtype: ident) => {
impl sea_orm::TryFromU64 for $newtype {
fn try_from_u64(_n: u64) -> Result<Self, sea_orm::DbErr> {
Err(sea_orm::ConvertFromU64(stringify!($newtype)))
Err(sea_orm::CannotConvertFromU64(stringify!($newtype)))
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion issues/400/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<T> From<AccountId<T>> for Uuid {

impl<T> sea_orm::TryFromU64 for AccountId<T> {
fn try_from_u64(_n: u64) -> Result<Self, sea_orm::DbErr> {
Err(sea_orm::ConvertFromU64(stringify!(AccountId<T>)))
Err(sea_orm::CannotConvertFromU64(stringify!(AccountId<T>)))
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ use thiserror::Error;
/// An error from unsuccessful database operations
#[derive(Error, Debug)]
pub enum DbErr {
/// This error happens, when a pool was not able to create a connection
/// This error can happen when the connection pool is fully-utilized
#[error("Failed to acquire connection from pool.")]
ConnFromPool,
/// Error in case of invalid type conversion attempts
#[error("fail to convert '{0}' into '{1}'")]
CannotConvertInto(String, String),
/// Error in case of invalid type conversion from an u64
#[error("{0} cannot be converted from u64")]
ConvertFromU64(String),
/// Runtime type conversion error
#[error("Error converting `{from}` into `{into}`: {source}")]
TryIntoErr {
/// From type
from: &'static str,
/// Into type
into: &'static str,
/// TryError
source: Box<dyn std::error::Error + Send>,
},
/// Type error: the specified type cannot be converted from u64. This is not a runtime error.
#[error("Type `{0}` cannot be converted from u64")]
CannotConvertFromU64(&'static str),
/// After an insert statement it was impossible to retrieve the last_insert_id
#[error("Fail to unpack last_insert_id")]
InsertCouldNotUnpackInsertId,
Expand Down
22 changes: 12 additions & 10 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,13 @@ impl TryGetable for Decimal {
let val: Option<f64> = row
.try_get(column.as_str())
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))?;
use rust_decimal::prelude::FromPrimitive;
match val {
Some(v) => Decimal::from_f64(v).ok_or_else(|| {
TryGetError::DbErr(DbErr::CannotConvertInto(
"f64".to_owned(),
"Decimal".to_owned(),
))
Some(v) => Decimal::try_from(v).map_err(|e| {
TryGetError::DbErr(DbErr::TryIntoErr {
from: "f64",
into: "Decimal",
source: Box::new(e),
})
}),
None => Err(TryGetError::Null(column)),
}
Expand Down Expand Up @@ -709,7 +709,7 @@ macro_rules! try_from_u64_err {
( $type: ty ) => {
impl TryFromU64 for $type {
fn try_from_u64(_: u64) -> Result<Self, DbErr> {
Err(DbErr::ConvertFromU64(stringify!($type).to_string()))
Err(DbErr::CannotConvertFromU64(stringify!($type)))
}
}
};
Expand All @@ -720,7 +720,7 @@ macro_rules! try_from_u64_err {
$( $gen_type: TryFromU64, )*
{
fn try_from_u64(_: u64) -> Result<Self, DbErr> {
Err(DbErr::ConvertFromU64(stringify!($($gen_type,)*).to_string()))
Err(DbErr::CannotConvertFromU64(stringify!($($gen_type,)*)))
}
}
};
Expand All @@ -738,8 +738,10 @@ macro_rules! try_from_u64_numeric {
impl TryFromU64 for $type {
fn try_from_u64(n: u64) -> Result<Self, DbErr> {
use std::convert::TryInto;
n.try_into().map_err(|_| {
DbErr::CannotConvertInto(n.to_string(), stringify!($type).to_string())
n.try_into().map_err(|e| DbErr::TryIntoErr {
from: stringify!(u64),
into: stringify!($type),
source: Box::new(e),
})
}
}
Expand Down

0 comments on commit 0b754ea

Please sign in to comment.