Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on error parsing RUST_LOG #1435

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions crates/fj-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod args;
mod config;
mod path;

use std::{env, error::Error};

use anyhow::{anyhow, Context};
use fj_export::export;
use fj_host::Parameters;
Expand All @@ -28,16 +30,9 @@ use tracing_subscriber::EnvFilter;
use crate::{args::Args, config::Config};

fn main() -> anyhow::Result<()> {
// Respect `RUST_LOG`. If that's not defined or erroneous, log warnings and
// above.
//
// It would be better to fail, if `RUST_LOG` is erroneous, but I don't know
// how to distinguish between that and the "not defined" case.
// Respect `RUST_LOG`. If that's not defined, log warnings and above. Fail if it's erroneous.
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("WARN")),
)
.with_env_filter(try_default_env_filter()?)
.event_format(format().pretty())
.init();

Expand Down Expand Up @@ -75,3 +70,29 @@ fn no_model_error() -> anyhow::Error {
- Specify a default model in the configuration file."
)
}

fn try_default_env_filter() -> anyhow::Result<EnvFilter> {
let env_filter = EnvFilter::try_from_default_env();

match env_filter {
Ok(env_filter) => Ok(env_filter),

Err(err) => {
if let Some(kind) = err.source() {
if let Some(env::VarError::NotPresent) =
kind.downcast_ref::<env::VarError>()
{
return Ok(EnvFilter::new("WARN"));
}
} else {
// `tracing_subscriber::filter::FromEnvError` currently returns a source
// in all cases.
unreachable!()
}

Err(anyhow!(
"There was an error parsing the RUST_LOG environment variable."
))
}
}
}