diff --git a/ingestion/src/lib.rs b/ingestion/src/lib.rs index 96ef298a..199e6c78 100644 --- a/ingestion/src/lib.rs +++ b/ingestion/src/lib.rs @@ -39,7 +39,12 @@ pub enum Error { #[error("RwLock was poisoned: {0}")] Lock(String), #[error("Could not read environment variable: {0}")] - Env(#[from] std::env::VarError), + Env(String), +} + +/// Gets an environment variable, providing more details than calling std::env::var() directly. +pub fn getenv(key: &str) -> Result { + std::env::var(key).map_err(|_| Error::Env(format!("Environment variable not found: {}", key))) } impl PartialEq for Error { diff --git a/ingestion/src/main.rs b/ingestion/src/main.rs index 6efebf08..82fa352c 100644 --- a/ingestion/src/main.rs +++ b/ingestion/src/main.rs @@ -4,7 +4,7 @@ use rove_connector::Connector; use std::sync::{Arc, RwLock}; use tokio_postgres::NoTls; -use lard_ingestion::permissions; +use lard_ingestion::{getenv, permissions}; const PARAMCONV: &str = "resources/paramconversions.csv"; @@ -27,7 +27,7 @@ async fn main() -> Result<(), Box> { // Set up postgres connection pool let manager = - PostgresConnectionManager::new_from_stringlike(std::env::var("LARD_CONN_STRING")?, NoTls)?; + PostgresConnectionManager::new_from_stringlike(getenv("LARD_CONN_STRING")?, NoTls)?; let db_pool = bb8::Pool::builder().build(manager).await?; // QC system diff --git a/ingestion/src/permissions.rs b/ingestion/src/permissions.rs index 4f2c7f18..277325c3 100644 --- a/ingestion/src/permissions.rs +++ b/ingestion/src/permissions.rs @@ -1,4 +1,4 @@ -use crate::Error; +use crate::{getenv, Error}; use std::{ collections::HashMap, sync::{Arc, RwLock}, @@ -41,8 +41,7 @@ pub type StationPermitTable = HashMap; /// Get a fresh cache of permits from stinfosys pub async fn fetch_permits() -> Result<(ParamPermitTable, StationPermitTable), Error> { // get stinfo conn - let (client, conn) = - tokio_postgres::connect(&std::env::var("STINFO_CONN_STRING")?, NoTls).await?; + let (client, conn) = tokio_postgres::connect(&getenv("STINFO_CONN_STRING")?, NoTls).await?; // conn object independently performs communication with database, so needs it's own task. // it will return when the client is dropped