chore: avoid to panic in case of an entity attribute is not found

This commit is contained in:
2026-03-16 10:09:24 +01:00
parent 66e7bc22cf
commit e98f6b7edf
2 changed files with 7 additions and 2 deletions

View File

@@ -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>`.
///

View File

@@ -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 => {