From f714773ba88e23d60b4fb2589a109000d27d556e Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Tue, 22 Apr 2025 10:44:25 +0200 Subject: [PATCH] Use custom error for missing environment variables --- src/lldap.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lldap.rs b/src/lldap.rs index 31b6e69..6e21840 100644 --- a/src/lldap.rs +++ b/src/lldap.rs @@ -1,6 +1,5 @@ use std::time::Duration; -use color_eyre::eyre::Context; use cynic::http::{CynicReqwestError, ReqwestExt}; use cynic::{GraphQlError, GraphQlResponse, MutationBuilder, QueryBuilder}; use lldap_auth::login::{ClientSimpleLoginRequest, ServerLoginResponse}; @@ -29,6 +28,8 @@ pub enum Error { Authentication(#[from] AuthenticationError), #[error("GraphQL error: {0}")] GraphQl(#[from] GraphQlError), + #[error("Missing environment variable: {0}")] + MissingEnvironmentVariable(&'static str), } pub type Result = std::result::Result; @@ -53,13 +54,14 @@ pub struct LldapConfig { } impl LldapConfig { - pub fn try_from_env() -> color_eyre::Result { + pub fn try_from_env() -> Result { Ok(Self { username: std::env::var("LLDAP_USERNAME") - .wrap_err("Variable 'LLDAP_USERNAME' is not set")?, + .map_err(|_| Error::MissingEnvironmentVariable("LLDAP_USERNAME"))?, password: std::env::var("LLDAP_PASSWORD") - .wrap_err("Variable 'LLDAP_PASSWORD' is not set")?, - url: std::env::var("LLDAP_URL").wrap_err("Variable 'LLDAP_URL' is not set")?, + .map_err(|_| Error::MissingEnvironmentVariable("LLDAP_PASSWORD"))?, + url: std::env::var("LLDAP_URL") + .map_err(|_| Error::MissingEnvironmentVariable("LLDAP_URL"))?, }) }