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

View file

@ -1,5 +1,5 @@
use igd::Gateway;
use anyhow::Result;
use anyhow::{Result, Context};
use crate::*;
@ -10,11 +10,11 @@ pub struct DiplonatContext {
pub fn setup() -> Result<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
});
}
pub fn dloop() -> bool {
pub fn listen() -> bool {
return true;
}

View file

@ -13,7 +13,8 @@ mod config;
async fn main() {
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 resp = reqwest::get(&url)