chore: avoid to panic in case of an entity attribute is not found
This commit is contained in:
@@ -141,14 +141,15 @@ impl Entity {
|
||||
/// # Returns
|
||||
///
|
||||
/// A `Result<T, FailedTo>` which is `Ok(T)` if the conversion is successful,
|
||||
/// or `Err(FailedTo::ConvertValue)` if it fails.
|
||||
/// or `Err(FailedTo::ConvertValue)` if the conversion fails,
|
||||
/// or `Err(FailedTo::FindAttribute)` if the attribute doesn't exist.
|
||||
pub fn unwrap<T>(&self, id: &str) -> Result<T, FailedTo>
|
||||
where
|
||||
T: TryFrom<Value>,
|
||||
{
|
||||
self.value_of(id)
|
||||
.map(|value| T::try_from(value.clone()).map_err(|_| FailedTo::ConvertValue))
|
||||
.unwrap()
|
||||
.unwrap_or(Err(FailedTo::FindAttribute))
|
||||
}
|
||||
/// Unwraps an attribute's value into an `Option<T>`.
|
||||
///
|
||||
|
||||
@@ -13,6 +13,8 @@ pub enum FailedTo {
|
||||
ConvertValue,
|
||||
/// Failed to execute predicate to mutate an item.
|
||||
ExecutePredicate(Vec<Box<dyn error::Error>>),
|
||||
/// Failed to find given attribute.
|
||||
FindAttribute,
|
||||
/// Failed to initialize the database.
|
||||
InitDatabase,
|
||||
/// Failed to load data from the database.
|
||||
@@ -38,6 +40,7 @@ impl PartialEq for FailedTo {
|
||||
| (FailedTo::ConvertObject, FailedTo::ConvertObject)
|
||||
| (FailedTo::ConvertValue, FailedTo::ConvertValue)
|
||||
| (FailedTo::ExecutePredicate(_), FailedTo::ExecutePredicate(_))
|
||||
| (FailedTo::FindAttribute, FailedTo::FindAttribute)
|
||||
| (FailedTo::InitDatabase, FailedTo::InitDatabase)
|
||||
| (FailedTo::LoadFromDB, FailedTo::LoadFromDB)
|
||||
| (FailedTo::LockCatalog, FailedTo::LockCatalog)
|
||||
@@ -74,6 +77,7 @@ impl std::fmt::Display for FailedTo {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
FailedTo::FindAttribute => write!(f, "Failed to find given attribute"),
|
||||
FailedTo::InitDatabase => write!(f, "Failed to initialize the database"),
|
||||
FailedTo::LoadFromDB => write!(f, "Failed to load data from the database"),
|
||||
FailedTo::LockCatalog => {
|
||||
|
||||
Reference in New Issue
Block a user