better environment variable errors

This commit is contained in:
Brandon Thomas committed 2024-01-28 03:36:17 -05:00
1 parent 6cdbe9ea00
commit 8d22697855
5 files changed
+21 -6

No files matched your search

+17 -2
View File
@@ -13,7 +13,10 @@ pub enum EnvError {
reason: String
},
/// The required environment variable wasn't present.
RequiredNotPresent,
RequiredNotPresent {
/// The name of the missing environment variable.
name: String
},
}
impl Display for EnvError {
@@ -21,7 +24,19 @@ impl Display for EnvError {
let reason = match self {
EnvError::NotUnicode => "EnvError::NotUnicode",
EnvError::ParseError { .. } => "EnvError::ParseError",
EnvError::RequiredNotPresent => "EnvError::RequiredNotPresent",
EnvError::RequiredNotPresent { name } =>
return write!(f, r#"
EnvError::RequiredNotPresent: the following environment variable was not present:
---> {:?}
In development, setting it in the environment config files: .env, .env-secrets,
cargo/service/../{{app_name}}/config/{{app_name}}.development.env, etc.
If it looks like it might be a secret, ask for help in our company Discord.
In production, make sure these environment variables are set in Kubernetes.
"#, name),
};
write!(f, "{:?}", reason)
}
+1 -1
View File
@@ -51,7 +51,7 @@ pub fn get_env_bool_required(env_name: &str) -> Result<bool, EnvError> {
.and_then(|maybe| match maybe {
None => {
warn!("Env var '{}' not supplied.", env_name);
Err(EnvError::RequiredNotPresent)
Err(EnvError::RequiredNotPresent { name: env_name.to_string() })
},
Some(val) => Ok(val),
})
+1 -1
View File
@@ -12,7 +12,7 @@ pub fn get_env_duration_seconds_required(env_name: &str) -> Result<Duration, Env
.and_then(|maybe| match maybe {
None => {
warn!("Env var '{}' not supplied.", env_name);
Err(EnvError::RequiredNotPresent)
Err(EnvError::RequiredNotPresent { name: env_name.to_string() })
},
Some(val) => Ok(val),
})
+1 -1
View File
@@ -15,7 +15,7 @@ pub fn get_env_pathbuf_required(env_name: &str) -> Result<PathBuf, EnvError> {
.and_then(|maybe| match maybe {
None => {
warn!("Env var '{}' not supplied.", env_name);
Err(EnvError::RequiredNotPresent)
Err(EnvError::RequiredNotPresent { name: env_name.to_string() })
},
Some(val) => Ok(val),
})
+1 -1
View File
@@ -32,7 +32,7 @@ pub fn get_env_string_required(env_name: &str) -> Result<String, EnvError> {
Some(s) => Ok(s.to_string()),
None => {
warn!("Required env var '{}' not supplied.", env_name);
Err(EnvError::RequiredNotPresent)
Err(EnvError::RequiredNotPresent { name: env_name.to_string() })
},
}
}