Refactor Type Errors

This commit is contained in:
Chris Tsang 2022-08-28 12:59:09 +08:00
parent fe6c40dd75
commit 0b754eab0b
4 changed files with 28 additions and 19 deletions

View File

@ -26,7 +26,7 @@ macro_rules! impl_try_from_u64_err {
($newtype: ident) => { ($newtype: ident) => {
impl sea_orm::TryFromU64 for $newtype { impl sea_orm::TryFromU64 for $newtype {
fn try_from_u64(_n: u64) -> Result<Self, sea_orm::DbErr> { fn try_from_u64(_n: u64) -> Result<Self, sea_orm::DbErr> {
Err(sea_orm::ConvertFromU64(stringify!($newtype))) Err(sea_orm::CannotConvertFromU64(stringify!($newtype)))
} }
} }
}; };

View File

@ -31,7 +31,7 @@ impl<T> From<AccountId<T>> for Uuid {
impl<T> sea_orm::TryFromU64 for AccountId<T> { impl<T> sea_orm::TryFromU64 for AccountId<T> {
fn try_from_u64(_n: u64) -> Result<Self, sea_orm::DbErr> { 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>)))
} }
} }

View File

@ -5,15 +5,22 @@ use thiserror::Error;
/// An error from unsuccessful database operations /// An error from unsuccessful database operations
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum DbErr { 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.")] #[error("Failed to acquire connection from pool.")]
ConnFromPool, ConnFromPool,
/// Error in case of invalid type conversion attempts /// Runtime type conversion error
#[error("fail to convert '{0}' into '{1}'")] #[error("Error converting `{from}` into `{into}`: {source}")]
CannotConvertInto(String, String), TryIntoErr {
/// Error in case of invalid type conversion from an u64 /// From type
#[error("{0} cannot be converted from u64")] from: &'static str,
ConvertFromU64(String), /// 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 /// After an insert statement it was impossible to retrieve the last_insert_id
#[error("Fail to unpack last_insert_id")] #[error("Fail to unpack last_insert_id")]
InsertCouldNotUnpackInsertId, InsertCouldNotUnpackInsertId,

View File

@ -376,13 +376,13 @@ impl TryGetable for Decimal {
let val: Option<f64> = row let val: Option<f64> = row
.try_get(column.as_str()) .try_get(column.as_str())
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))?; .map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))?;
use rust_decimal::prelude::FromPrimitive;
match val { match val {
Some(v) => Decimal::from_f64(v).ok_or_else(|| { Some(v) => Decimal::try_from(v).map_err(|e| {
TryGetError::DbErr(DbErr::CannotConvertInto( TryGetError::DbErr(DbErr::TryIntoErr {
"f64".to_owned(), from: "f64",
"Decimal".to_owned(), into: "Decimal",
)) source: Box::new(e),
})
}), }),
None => Err(TryGetError::Null(column)), None => Err(TryGetError::Null(column)),
} }
@ -709,7 +709,7 @@ macro_rules! try_from_u64_err {
( $type: ty ) => { ( $type: ty ) => {
impl TryFromU64 for $type { impl TryFromU64 for $type {
fn try_from_u64(_: u64) -> Result<Self, DbErr> { fn try_from_u64(_: u64) -> Result<Self, DbErr> {
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, )* $( $gen_type: TryFromU64, )*
{ {
fn try_from_u64(_: u64) -> Result<Self, DbErr> { fn try_from_u64(_: u64) -> Result<Self, DbErr> {
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 { impl TryFromU64 for $type {
fn try_from_u64(n: u64) -> Result<Self, DbErr> { fn try_from_u64(n: u64) -> Result<Self, DbErr> {
use std::convert::TryInto; use std::convert::TryInto;
n.try_into().map_err(|_| { n.try_into().map_err(|e| DbErr::TryIntoErr {
DbErr::CannotConvertInto(n.to_string(), stringify!($type).to_string()) from: stringify!(u64),
into: stringify!($type),
source: Box::new(e),
}) })
} }
} }