errors: use anyhow::Context everywhere

This commit is contained in:
Valentin Tolmer
2021-08-26 21:56:42 +02:00
committed by nitnelave
parent a08b9a556d
commit 83ed58bff2
5 changed files with 23 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::{Context, Result};
use figment::{
providers::{Env, Format, Serialized, Toml},
Figment,
@@ -93,19 +93,16 @@ fn get_server_setup(file_path: &str) -> Result<ServerSetup> {
use std::path::Path;
let path = Path::new(file_path);
if path.exists() {
let bytes = std::fs::read(file_path)
.map_err(|e| anyhow!("Could not read key file `{}`: {}", file_path, e))?;
let bytes =
std::fs::read(file_path).context(format!("Could not read key file `{}`", file_path))?;
Ok(ServerSetup::deserialize(&bytes)?)
} else {
let mut rng = rand::rngs::OsRng;
let server_setup = ServerSetup::new(&mut rng);
std::fs::write(path, server_setup.serialize()).map_err(|e| {
anyhow!(
"Could not write the generated server setup to file `{}`: {}",
file_path,
e
)
})?;
std::fs::write(path, server_setup.serialize()).context(format!(
"Could not write the generated server setup to file `{}`",
file_path,
))?;
Ok(server_setup)
}
}

View File

@@ -9,7 +9,7 @@ use crate::{
infra::{cli::*, configuration::Configuration, db_cleaner::Scheduler},
};
use actix::Actor;
use anyhow::{anyhow, Result};
use anyhow::{Context, Result};
use futures_util::TryFutureExt;
use log::*;
@@ -24,20 +24,20 @@ async fn create_admin_user(handler: &SqlBackendHandler, config: &Configuration)
})
.and_then(|_| register_password(handler, &config.ldap_user_dn, &config.ldap_user_pass))
.await
.map_err(|e| anyhow!("Error creating admin user: {}", e))?;
.context("Error creating admin user")?;
let admin_group_id = handler
.create_group(lldap_model::CreateGroupRequest {
display_name: "lldap_admin".to_string(),
})
.await
.map_err(|e| anyhow!("Error creating admin group: {}", e))?;
.context("Error creating admin group")?;
handler
.add_user_to_group(lldap_model::AddUserToGroupRequest {
user_id: config.ldap_user_dn.clone(),
group_id: admin_group_id,
})
.await
.map_err(|e| anyhow!("Error adding admin user to group: {}", e))
.context("Error adding admin user to group")
}
async fn run_server(config: Configuration) -> Result<()> {