Prevent nested error messages

⚓ rust    📅 2025-05-05    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 60 days ago. The information described in this article may have changed.

I have multiple error logs that looks like this message Failed to fetch dataset for data collection game: DbErr(RecordNotFound("Cannot find collected additional data")), continue...

This happens because I do multiple error handlings in different nested functions. How can I make these error messages more cleaner?

This is one example that I have:

pub async fn get_additional_collection(
    con: &DatabaseConnection,
    user_id: i32
) -> Result<additional_collection::Model, sea_orm::DbErr> {

    let additional_collection = AdditionalCollection::find()
        .filter(additional_collection::Column::UserId.eq(user_id))
        .one(con)
        .await?;

    if let Some(col) = additional_collection {
        Ok(col)
    } else {
        Err(sea_orm::DbErr::RecordNotFound("Cannot find collected additional data".to_string()))
    }
}
pub async fn get_class_datasets(
    con: &DatabaseConnection,
    class_id: i32
) -> Result<Vec<String>, error_handler::IsumisError> {

    let class_name = class_db::get_class_by_id(con, class_id).await?;
    let users = user_db::get_users(con, class_name.class_name).await?;

    let datasets= {

        let mut datasets = Vec::new();

        for user in users.into_iter() {
            if let Err(err) = create_dataset(con, user.id).await {
                warn!("Failed to fetch dataset for data collection game: {}, continue...", err);
            } else {
                let dataset = create_dataset(con, user.id).await?;

                datasets.push(dataset.to_string());
            }
        }

        datasets

    };

    Ok(datasets)
}

3 posts - 3 participants

Read full topic

🏷️ rust_feed