Add DbErr::Type

This commit is contained in:
Billy Chan 2021-10-20 16:41:01 +08:00
parent 388bf2cfca
commit f1ef7d9c47
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
3 changed files with 22 additions and 8 deletions

View File

@ -191,7 +191,7 @@ impl ActiveEnum {
fn try_from_value(v: &Self::Value) -> Result<Self, sea_orm::DbErr> {
match #val {
#( #variant_values => Ok(Self::#variant_idents), )*
_ => Err(sea_orm::DbErr::Query(format!(
_ => Err(sea_orm::DbErr::Type(format!(
"unexpected value for {} enum: {}",
stringify!(#ident),
v

View File

@ -51,7 +51,7 @@ use sea_query::{Nullable, Value, ValueType};
/// match v.as_ref() {
/// "B" => Ok(Self::Big),
/// "S" => Ok(Self::Small),
/// _ => Err(DbErr::Query(format!(
/// _ => Err(DbErr::Type(format!(
/// "unexpected value for Category enum: {}",
/// v
/// ))),
@ -138,7 +138,7 @@ mod tests {
match v.as_ref() {
"B" => Ok(Self::Big),
"S" => Ok(Self::Small),
_ => Err(DbErr::Query(format!(
_ => Err(DbErr::Type(format!(
"unexpected value for Category enum: {}",
v
))),
@ -166,7 +166,7 @@ mod tests {
assert_eq!(
Category::try_from_value(&"A".to_owned()).err(),
Some(DbErr::Query(
Some(DbErr::Type(
"unexpected value for Category enum: A".to_owned()
))
);
@ -180,7 +180,7 @@ mod tests {
);
assert_eq!(
DeriveCategory::try_from_value(&"A".to_owned()).err(),
Some(DbErr::Query(
Some(DbErr::Type(
"unexpected value for DeriveCategory enum: A".to_owned()
))
);
@ -221,7 +221,7 @@ mod tests {
assert_eq!($ident::try_from_value(&-10).ok(), Some($ident::Negative));
assert_eq!(
$ident::try_from_value(&2).err(),
Some(DbErr::Query(format!(
Some(DbErr::Type(format!(
"unexpected value for {} enum: 2",
stringify!($ident)
)))
@ -257,7 +257,7 @@ mod tests {
assert_eq!($ident::try_from_value(&0).ok(), Some($ident::Small));
assert_eq!(
$ident::try_from_value(&2).err(),
Some(DbErr::Query(format!(
Some(DbErr::Type(format!(
"unexpected value for {} enum: 2",
stringify!($ident)
)))

View File

@ -1,10 +1,23 @@
/// Represents all the errors in SeaORM.
#[derive(Debug, PartialEq)]
pub enum DbErr {
/// Error occurred while connecting to database engine.
Conn(String),
/// Error occurred while executing SQL statement.
Exec(String),
/// Error occurred while querying SQL statement.
Query(String),
/// Error occurred while updating a non-existing row in database.
RecordNotFound(String),
/// Error occurred while performing custom validation logics in [ActiveModelBehavior](crate::ActiveModelBehavior)
Custom(String),
/// Error occurred while parsing value into [ActiveEnum](crate::ActiveEnum)
Type(String),
}
impl std::error::Error for DbErr {}
@ -17,6 +30,7 @@ impl std::fmt::Display for DbErr {
Self::Query(s) => write!(f, "Query Error: {}", s),
Self::RecordNotFound(s) => write!(f, "RecordNotFound Error: {}", s),
Self::Custom(s) => write!(f, "Custom Error: {}", s),
Self::Type(s) => write!(f, "Type Error: {}", s),
}
}
}