Refactor Type Errors
This commit is contained in:
parent
fe6c40dd75
commit
0b754eab0b
@ -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)))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -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>)))
|
||||
}
|
||||
}
|
||||
|
||||
|
21
src/error.rs
21
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<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,
|
||||
|
@ -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)),
|
||||
}
|
||||
@ -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)))
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -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,)*)))
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -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),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user