Optimize config loading messages

As kinda discussed here #3090, the messages regarding loading the
configuration files is a bit strange or unclear. There have been some
other reports regarding this in the past, but wasn't that big a of a
deal.

But to make the whole process it bit more nice, this PR adjusts the way
it reports issues and some small changes to the messages to make it all
a bit more clear.

- Do not report a missing `.env` file, but only send a message when using one.
- Exit instead of Panic, a panic causes a stacktrace, which isn't needed
  here. I'm using a exit code 255 here so it is different to the other
  exit's we use.
- Exit on more issues, since if we continue, it could cause
  configuration issues if the user thinks all is fine.
- Use the actual env file used in the messages instead of `.env`.
- Added a **INFO** message when loading the `config.json`.
  This makes it consistent with the info message for loading the env file.

Resolves #3090
This commit is contained in:
BlackDex 2023-01-02 18:18:28 +01:00 committed by Daniel García
parent f1c0aa4f83
commit e945d16fcf
No known key found for this signature in database
GPG Key ID: FC8A7D14C3CD543A

View File

@ -60,25 +60,37 @@ macro_rules! make_config {
impl ConfigBuilder { impl ConfigBuilder {
#[allow(clippy::field_reassign_with_default)] #[allow(clippy::field_reassign_with_default)]
fn from_env() -> Self { fn from_env() -> Self {
match dotenvy::from_path(get_env("ENV_FILE").unwrap_or_else(|| String::from(".env"))) { let env_file = get_env("ENV_FILE").unwrap_or_else(|| String::from(".env"));
Ok(_) => (), match dotenvy::from_path(&env_file) {
Ok(_) => {
println!("[INFO] Using environment file `{env_file}` for configuration.\n");
},
Err(e) => match e { Err(e) => match e {
dotenvy::Error::LineParse(msg, pos) => { dotenvy::Error::LineParse(msg, pos) => {
panic!("Error loading the .env file:\nNear {:?} on position {}\nPlease fix and restart!\n", msg, pos); println!("[ERROR] Failed parsing environment file: `{env_file}`\nNear {msg:?} on position {pos}\nPlease fix and restart!\n");
exit(255);
}, },
dotenvy::Error::Io(ioerr) => match ioerr.kind() { dotenvy::Error::Io(ioerr) => match ioerr.kind() {
std::io::ErrorKind::NotFound => { std::io::ErrorKind::NotFound => {
println!("[INFO] No .env file found.\n"); // Only exit if this environment variable is set, but the file was not found.
// This prevents incorrectly configured environments.
if let Some(env_file) = get_env::<String>("ENV_FILE") {
println!("[ERROR] The configured ENV_FILE `{env_file}` was not found!\n");
exit(255);
}
}, },
std::io::ErrorKind::PermissionDenied => { std::io::ErrorKind::PermissionDenied => {
println!("[WARNING] Permission Denied while trying to read the .env file!\n"); println!("[ERROR] Permission denied while trying to read environment file `{env_file}`!\n");
exit(255);
}, },
_ => { _ => {
println!("[WARNING] Reading the .env file failed:\n{:?}\n", ioerr); println!("[ERROR] Reading environment file `{env_file}` failed:\n{ioerr:?}\n");
exit(255);
} }
}, },
_ => { _ => {
println!("[WARNING] Reading the .env file failed:\n{:?}\n", e); println!("[ERROR] Reading environment file `{env_file}` failed:\n{e:?}\n");
exit(255);
} }
} }
}; };
@ -93,6 +105,7 @@ macro_rules! make_config {
fn from_file(path: &str) -> Result<Self, Error> { fn from_file(path: &str) -> Result<Self, Error> {
let config_str = std::fs::read_to_string(path)?; let config_str = std::fs::read_to_string(path)?;
println!("[INFO] Using saved config from `{path}` for configuration.\n");
serde_json::from_str(&config_str).map_err(Into::into) serde_json::from_str(&config_str).map_err(Into::into)
} }
@ -112,8 +125,8 @@ macro_rules! make_config {
if show_overrides && !overrides.is_empty() { if show_overrides && !overrides.is_empty() {
// We can't use warn! here because logging isn't setup yet. // We can't use warn! here because logging isn't setup yet.
println!("[WARNING] The following environment variables are being overriden by the config file,"); println!("[WARNING] The following environment variables are being overriden by the config.json file.");
println!("[WARNING] please use the admin panel to make changes to them:"); println!("[WARNING] Please use the admin panel to make changes to them:");
println!("[WARNING] {}\n", overrides.join(", ")); println!("[WARNING] {}\n", overrides.join(", "));
} }