diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-05-09 16:19:09 +0200 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-05-09 16:19:09 +0200 |
commit | 41caf6090ca8b24c162946054c48d59387d21200 (patch) | |
tree | 975b4f0b3928ddc0b353e3b80ed6f169a4e42509 /src/config.rs | |
parent | 76c84042120b97ee98af6a1c40ab86aca062408a (diff) | |
download | diplonat-41caf6090ca8b24c162946054c48d59387d21200.tar.gz diplonat-41caf6090ca8b24c162946054c48d59387d21200.zip |
Add context to errors
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/config.rs b/src/config.rs index 2cf2061..14d18be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,5 @@ use std::env; -use anyhow::{Result, anyhow}; +use anyhow::{Result, Context, anyhow}; use log::*; pub struct DiplonatConfig { @@ -10,16 +10,24 @@ pub struct DiplonatConfig { } pub fn load_env() -> Result<DiplonatConfig> { - let env_private_ip = "DIPLONAT_PRIVATE_IP"; - let env_refresh_time = "DIPLONAT_REFRESH_TIME"; - let env_expiration_time = "DIPLONAT_EXPIRATION_TIME"; - let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME"; + let epi = "DIPLONAT_PRIVATE_IP"; + let ert = "DIPLONAT_REFRESH_TIME"; + let eet = "DIPLONAT_EXPIRATION_TIME"; + let ecnd = "DIPLONAT_CONSUL_NODE_NAME"; let config = DiplonatConfig { - private_ip: env::var(env_private_ip)?, - refresh_time: env::var(env_refresh_time)?.parse()?, - expiration_time: env::var(env_expiration_time)?.parse()?, - consul_node_name: env::var(env_consul_node_name)? + private_ip: env::var(epi) + .with_context(|| format!("{} env var must be defined, eg: 192.168.0.18", epi))?, + refresh_time: env::var(ert) + .with_context(|| format!("{} env var must be defined, eg: 60", ert))? + .parse() + .with_context(|| format!("{} env var must be an integer, eg: 60", ert))?, + expiration_time: env::var(eet) + .with_context(|| format!("{} env var must be defined, eg: 300", eet))? + .parse() + .with_context(|| format!("{} env var must be an integer, eg: 300", eet))?, + consul_node_name: env::var(ecnd) + .with_context(|| format!("{} env var must be defined", ecnd))? }; if config.refresh_time * 2 > config.expiration_time { |