Skip to content

Commit

Permalink
better error messages on config load failure;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jul 19, 2023
1 parent c692ca0 commit 5d73244
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
7 changes: 4 additions & 3 deletions core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn main() {
};

let config_log_level = ORACLE_CONFIG_OPT
.clone()
.as_ref()
.map(|c| c.log_level)
.ok()
.flatten();
Expand Down Expand Up @@ -603,9 +603,10 @@ fn log_and_continue_if_non_fatal(

fn log_on_launch() {
log::info!("{}", APP_VERSION);
if let Ok(config) = ORACLE_CONFIG_OPT.clone() {
let oracle_address_opt = ORACLE_CONFIG_OPT.as_ref().map(|c| c.oracle_address.clone());
if let Ok(oracle_address) = oracle_address_opt {
// log::info!("Token ids: {:?}", config.token_ids);
log::info!("Oracle address: {}", config.oracle_address.to_base58());
log::info!("Oracle address: {}", oracle_address.to_base58());
}
}

Expand Down
20 changes: 13 additions & 7 deletions core/src/oracle_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::Context;
use ergo_lib::{
ergotree_ir::chain::address::NetworkAddress,
ergotree_ir::{
Expand Down Expand Up @@ -59,14 +60,20 @@ impl OracleConfig {
file.write_all(yaml_str.as_bytes()).unwrap();
}

fn load() -> Result<Self, OracleConfigFileError> {
fn load() -> Result<Self, anyhow::Error> {
let config_file_path = ORACLE_CONFIG_FILE_PATH.get().ok_or_else(|| {
OracleConfigFileError::IoError("ORACLE_CONFIG_FILE_PATH not set".to_string())
})?;
let config_str: &str = &std::fs::read_to_string(config_file_path)
.map_err(|e| OracleConfigFileError::IoError(e.to_string()))?;
let config = Self::load_from_str(config_str)?;
let _ = config.oracle_address_p2pk()?;
log::info!("Loading oracle config from {}", config_file_path.display());
let config_str = std::fs::read_to_string(config_file_path).context(format!(
"failed to load oracle config file from {}",
config_file_path.display()
))?;
let config =
Self::load_from_str(&config_str).context("failed to parse oracle config file")?;
let _ = config
.oracle_address_p2pk()
.context("failed to parse oracle address")?;
Ok(config)
}

Expand Down Expand Up @@ -124,8 +131,7 @@ pub static ORACLE_CONFIG_FILE_PATH: sync::OnceCell<PathBuf> = sync::OnceCell::ne
lazy_static! {
pub static ref ORACLE_CONFIG: OracleConfig = OracleConfig::load().unwrap();
pub static ref ORACLE_SECRETS: OracleSecrets = OracleSecrets::load();
pub static ref ORACLE_CONFIG_OPT: Result<OracleConfig, OracleConfigFileError> =
OracleConfig::load();
pub static ref ORACLE_CONFIG_OPT: Result<OracleConfig, anyhow::Error> = OracleConfig::load();
pub static ref BASE_FEE: BoxValue = ORACLE_CONFIG_OPT
.as_ref()
.map(|c| BoxValue::try_from(c.base_fee).unwrap())
Expand Down
10 changes: 8 additions & 2 deletions core/src/pool_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::Path;
use std::path::PathBuf;

use anyhow::anyhow;
use anyhow::Context;
use once_cell::sync;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -158,7 +159,12 @@ impl PoolConfig {
let config_file_path = POOL_CONFIG_FILE_PATH
.get()
.ok_or_else(|| anyhow!("Pool config file path not set"))?;
Self::load_from_str(&std::fs::read_to_string(config_file_path)?)
log::info!("Loading pool config from {}", config_file_path.display());
let config_str = std::fs::read_to_string(config_file_path).context(format!(
"failed to load pool config file from {}",
config_file_path.display()
))?;
Self::load_from_str(&config_str)
}

pub fn save(&self, path: &Path) -> Result<(), anyhow::Error> {
Expand All @@ -168,7 +174,7 @@ impl PoolConfig {
}

pub fn load_from_str(config_str: &str) -> Result<PoolConfig, anyhow::Error> {
serde_yaml::from_str(config_str).map_err(|e| anyhow!(e))
serde_yaml::from_str(config_str).context("failed to parse pool config file")
}
}

Expand Down

0 comments on commit 5d73244

Please sign in to comment.