Skip to content

Commit

Permalink
feat: new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed Jul 7, 2023
1 parent 2701bfb commit 896fa6d
Show file tree
Hide file tree
Showing 11 changed files with 540 additions and 132 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ pub fn parse<C: config::Config + 'static>(
}
builder.add_source_with_priority(source, 1);
}
Err(config::ConfigError::File(e)) => {
if key_provided {
return Err(e.into());
Err(err) => {
if key_provided || !err.is_io() {
return Err(err.into());
}
tracing::debug!("failed to load config file: {}", e);

tracing::debug!("failed to load config file: {}", err);
}
Err(e) => return Err(e.into()),
}
}

Expand Down
4 changes: 3 additions & 1 deletion config/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ toml = "0"
clap = { version = "4", features = ["cargo", "string"] }
convert_case = "0"
serde = { version = "1", features = ["derive"] }
serde-value = "0"
tracing = { version = "0" }
serde_ignored = "0"
serde-value = "0"
serde_path_to_error = "0"

# Derive macro
config_derive = { path = "../config_derive" }
Expand Down
29 changes: 22 additions & 7 deletions config/config/example/derive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Run with: `cargo run --example derive`
//! Look at the generated code with: `cargo expand --example derive`

use config::{sources, ConfigBuilder};
use config::{sources, ConfigBuilder, ConfigError};

type TypeAlias = bool;

Expand All @@ -14,7 +14,7 @@ struct AppConfig {
}

#[derive(config::Config, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
// #[serde(default)]
struct LoggingConfig {
level: String,
json: bool,
Expand All @@ -30,11 +30,26 @@ impl Default for LoggingConfig {
}

fn main() {
let mut builder: ConfigBuilder<AppConfig> = ConfigBuilder::new();
builder.add_source(sources::CliSource::new().unwrap());
match parse() {
Ok(config) => println!("{:#?}", config),
Err(err) => println!("{:#}", err),
}
}

match builder.build() {
Ok(config) => println!("{:?}", config),
Err(e) => eprintln!("error: {:#}", e),
fn parse() -> Result<AppConfig, ConfigError> {
let mut builder = ConfigBuilder::new();
builder.add_source(sources::CliSource::new()?);
builder.add_source(sources::EnvSource::with_prefix("TEST")?);
builder.add_source(sources::FileSource::json(
br#"
{
"optional": null
}
"#
.as_slice(),
)?);

let config: AppConfig = builder.build()?;

Ok(config)
}
Loading

0 comments on commit 896fa6d

Please sign in to comment.