diff --git a/issues/324/src/model.rs b/issues/324/src/model.rs index 37174db9..940bc9ca 100644 --- a/issues/324/src/model.rs +++ b/issues/324/src/model.rs @@ -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 { - Err(sea_orm::ConvertFromU64(stringify!($newtype))) + Err(sea_orm::CannotConvertFromU64(stringify!($newtype))) } } }; diff --git a/issues/400/src/model.rs b/issues/400/src/model.rs index 6d9044c4..25e4732f 100644 --- a/issues/400/src/model.rs +++ b/issues/400/src/model.rs @@ -31,7 +31,7 @@ impl From> for Uuid { impl sea_orm::TryFromU64 for AccountId { fn try_from_u64(_n: u64) -> Result { - Err(sea_orm::ConvertFromU64(stringify!(AccountId))) + Err(sea_orm::CannotConvertFromU64(stringify!(AccountId))) } } diff --git a/src/error.rs b/src/error.rs index 30b2909d..6d594095 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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, + }, + /// 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, diff --git a/src/executor/query.rs b/src/executor/query.rs index 0f1b22b2..c2d0a051 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -376,13 +376,13 @@ impl TryGetable for Decimal { let val: Option = 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)), } @@ -709,7 +709,7 @@ macro_rules! try_from_u64_err { ( $type: ty ) => { impl TryFromU64 for $type { fn try_from_u64(_: u64) -> Result { - Err(DbErr::ConvertFromU64(stringify!($type).to_string())) + Err(DbErr::CannotConvertFromU64(stringify!($type))) } } }; @@ -720,7 +720,7 @@ macro_rules! try_from_u64_err { $( $gen_type: TryFromU64, )* { fn try_from_u64(_: u64) -> Result { - Err(DbErr::ConvertFromU64(stringify!($($gen_type,)*).to_string())) + Err(DbErr::CannotConvertFromU64(stringify!($($gen_type,)*))) } } }; @@ -738,8 +738,10 @@ macro_rules! try_from_u64_numeric { impl TryFromU64 for $type { fn try_from_u64(n: u64) -> Result { 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), }) } }