Skip to content

Commit

Permalink
Add DbErr::Type
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Oct 20, 2021
1 parent 388bf2c commit f1ef7d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sea-orm-macros/src/derives/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sea_query::{Nullable, Value, ValueType};
///
/// ```rust
/// use sea_orm::entity::prelude::*;
///
///
/// // Using the derive macro
/// #[derive(Debug, PartialEq, DeriveActiveEnum)]
/// #[sea_orm(rs_type = "String", db_type = "String(Some(1))")]
Expand Down Expand Up @@ -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
/// ))),
Expand Down Expand Up @@ -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
))),
Expand Down Expand Up @@ -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()
))
);
Expand All @@ -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()
))
);
Expand Down Expand Up @@ -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)
)))
Expand Down Expand Up @@ -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)
)))
Expand Down
14 changes: 14 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -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),
}
}
}
Expand Down

0 comments on commit f1ef7d9

Please sign in to comment.