Add context to errors

This commit is contained in:
Quentin 2020-05-09 16:19:09 +02:00
parent 76c8404212
commit 41caf6090c
3 changed files with 22 additions and 13 deletions

View file

@ -1,5 +1,5 @@
use std::env; use std::env;
use anyhow::{Result, anyhow}; use anyhow::{Result, Context, anyhow};
use log::*; use log::*;
pub struct DiplonatConfig { pub struct DiplonatConfig {
@ -10,16 +10,24 @@ pub struct DiplonatConfig {
} }
pub fn load_env() -> Result<DiplonatConfig> { pub fn load_env() -> Result<DiplonatConfig> {
let env_private_ip = "DIPLONAT_PRIVATE_IP"; let epi = "DIPLONAT_PRIVATE_IP";
let env_refresh_time = "DIPLONAT_REFRESH_TIME"; let ert = "DIPLONAT_REFRESH_TIME";
let env_expiration_time = "DIPLONAT_EXPIRATION_TIME"; let eet = "DIPLONAT_EXPIRATION_TIME";
let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME"; let ecnd = "DIPLONAT_CONSUL_NODE_NAME";
let config = DiplonatConfig { let config = DiplonatConfig {
private_ip: env::var(env_private_ip)?, private_ip: env::var(epi)
refresh_time: env::var(env_refresh_time)?.parse()?, .with_context(|| format!("{} env var must be defined, eg: 192.168.0.18", epi))?,
expiration_time: env::var(env_expiration_time)?.parse()?, refresh_time: env::var(ert)
consul_node_name: env::var(env_consul_node_name)? .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 { if config.refresh_time * 2 > config.expiration_time {

View file

@ -1,5 +1,5 @@
use igd::Gateway; use igd::Gateway;
use anyhow::Result; use anyhow::{Result, Context};
use crate::*; use crate::*;
@ -10,11 +10,11 @@ pub struct DiplonatContext {
pub fn setup() -> Result<DiplonatContext> { pub fn setup() -> Result<DiplonatContext> {
return Ok(DiplonatContext { return Ok(DiplonatContext {
config: config::load_env()?, config: config::load_env().context("Unable to read configuration from environment")?,
//gateway: search_gateway(Default::default()).await //gateway: search_gateway(Default::default()).await
}); });
} }
pub fn dloop() -> bool { pub fn listen() -> bool {
return true; return true;
} }

View file

@ -13,7 +13,8 @@ mod config;
async fn main() { async fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
let ctx = diplonat::setup().expect("Setup failed:"); let ctx = diplonat::setup().expect("Setup failed");
diplonat::listen();
/* /*
let url = format!("http://127.0.0.1:8500/v1/catalog/node/{}", config.consul_node_name); let url = format!("http://127.0.0.1:8500/v1/catalog/node/{}", config.consul_node_name);
let resp = reqwest::get(&url) let resp = reqwest::get(&url)