dotenv_vault/
errors.rs

1use std::{error, fmt};
2
3#[derive(Debug)]
4#[non_exhaustive]
5pub enum Error {
6    KeyNotFound,
7    VaultNotFound,
8    DotenvyError(dotenvy::Error),
9    ParseError(url::ParseError),
10    InvalidScheme,
11    MissingKey,
12    MissingEnvironment,
13    EnvironmentNotFound(String),
14    InvalidKey,
15    HexError(hex::FromHexError),
16    DecodeError(base64::DecodeError),
17    DecryptError(aes_gcm::Error),
18}
19
20pub type Result<T> = std::result::Result<T, Error>;
21
22impl fmt::Display for Error {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match *self {
25            Error::KeyNotFound => {
26                write!(
27                    f,
28                    "NOT_FOUND_DOTENV_KEY: Cannot find environment variable 'DOTENV_KEY'"
29                )
30            }
31            Error::VaultNotFound => {
32                write!(f, "NOT_FOUND_DOTENV_VAULT: Cannot find vault file")
33            }
34            Error::DotenvyError(ref error) => error.fmt(f),
35            Error::ParseError(_) => {
36                write!(f, "INVALID_DOTENV_KEY: Failed to parse url")
37            }
38            Error::InvalidScheme => {
39                write!(f, "INVALID_DOTENV_KEY: Invalid scheme")
40            }
41            Error::MissingKey => {
42                write!(f, "INVALID_DOTENV_KEY: Missing key part")
43            }
44            Error::MissingEnvironment => {
45                write!(f, "INVALID_DOTENV_KEY: Missing environment part")
46            }
47            Error::EnvironmentNotFound(ref environment) => {
48                write!(f, "NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment {} in your .env.vault file. Run 'npx dotenv-vault build' to include it.", environment)
49            }
50            Error::InvalidKey => {
51                write!(f, "INVALID_DOTENV_KEY: Key must be valid")
52            }
53            Error::HexError(_) => {
54                write!(f, "INVALID_DOTENV_KEY: Failed to decode hex string")
55            }
56            Error::DecodeError(_) => {
57                write!(f, "DECRYPTION_FAILED: Failed to decode base64 string")
58            }
59            Error::DecryptError(_) => {
60                write!(f, "DECRYPTION_FAILED: Please check your DOTENV_KEY")
61            }
62        }
63    }
64}
65
66impl error::Error for Error {
67    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
68        match *self {
69            Error::KeyNotFound => None,
70            Error::VaultNotFound => None,
71            Error::DotenvyError(ref e) => Some(e),
72            Error::ParseError(ref e) => Some(e),
73            Error::InvalidScheme => None,
74            Error::MissingKey => None,
75            Error::MissingEnvironment => None,
76            Error::EnvironmentNotFound(_) => None,
77            Error::InvalidKey => None,
78            Error::HexError(ref e) => Some(e),
79            Error::DecodeError(ref e) => Some(e),
80            Error::DecryptError(_) => None,
81        }
82    }
83}
84
85impl From<dotenvy::Error> for Error {
86    fn from(err: dotenvy::Error) -> Error {
87        Error::DotenvyError(err)
88    }
89}
90
91impl From<hex::FromHexError> for Error {
92    fn from(err: hex::FromHexError) -> Error {
93        Error::HexError(err)
94    }
95}
96
97impl From<base64::DecodeError> for Error {
98    fn from(err: base64::DecodeError) -> Error {
99        Error::DecodeError(err)
100    }
101}
102
103impl From<aes_gcm::Error> for Error {
104    fn from(err: aes_gcm::Error) -> Error {
105        Error::DecryptError(err)
106    }
107}
108
109impl From<url::ParseError> for Error {
110    fn from(err: url::ParseError) -> Error {
111        Error::ParseError(err)
112    }
113}