better error message for config issues

This commit is contained in:
Marc Planard 2023-06-08 20:34:09 +02:00
parent 9c996b1029
commit a791514cbe
2 changed files with 8 additions and 2 deletions

View File

@ -1,6 +1,6 @@
use config::Config; use config::Config;
use serde::{Serialize,Deserialize}; use serde::{Serialize,Deserialize};
use crate::errors::LJResult; use crate::errors::{LJError,LJResult};
use crate::transformer; use crate::transformer;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
@ -46,7 +46,7 @@ impl Conf {
.add_source(config::File::with_name(path)) .add_source(config::File::with_name(path))
.build()?; .build()?;
let conf : Conf = settings.try_deserialize()?; let conf : Conf = settings.try_deserialize().map_err(LJError::Config)?;
Ok(conf) Ok(conf)
} }

View File

@ -1,11 +1,13 @@
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use redis::RedisError; use redis::RedisError;
use config::ConfigError;
pub type LJResult<T> = Result<T, Box<dyn std::error::Error>>; pub type LJResult<T> = Result<T, Box<dyn std::error::Error>>;
#[derive(Debug)] #[derive(Debug)]
pub enum LJError { pub enum LJError {
Config(ConfigError),
RedisConnect(RedisError), RedisConnect(RedisError),
HeliosDeviceMissing HeliosDeviceMissing
} }
@ -15,6 +17,10 @@ impl fmt::Display for LJError {
use LJError::*; use LJError::*;
match self { match self {
Config(err) => {
write!(f, "unable to load config file: {err}")
},
RedisConnect(err) => { RedisConnect(err) => {
write!(f, "unable to connect to redis server: {err}") write!(f, "unable to connect to redis server: {err}")
}, },