Skip to main content

lightshuttle_secrets/
error.rs

1//! Error types for secret loading.
2
3use std::path::PathBuf;
4
5/// Errors that can occur while loading secrets from a source.
6///
7/// These errors are typically encountered when loading a `.env` file via [`crate::EnvFileSource`].
8/// Use pattern matching to distinguish between missing files, I/O errors, and parse errors.
9#[derive(Debug, thiserror::Error)]
10pub enum SecretError {
11    /// The `.env` file path was explicitly provided but does not exist.
12    ///
13    /// This error is returned by [`crate::EnvFileSource::load`] when the file is not found.
14    /// Use [`crate::EnvFileSource::load_optional`] instead if the file is allowed to be absent.
15    #[error("env file not found: {0}")]
16    FileNotFound(PathBuf),
17
18    /// An I/O error occurred while reading the file.
19    ///
20    /// This wraps the underlying filesystem error. Common causes include permission denied,
21    /// invalid path, or disk read failures.
22    #[error("failed to read env file {path}: {source}")]
23    Io {
24        /// Path to the file that caused the error.
25        path: PathBuf,
26        /// Underlying I/O error from the filesystem.
27        #[source]
28        source: std::io::Error,
29    },
30
31    /// A line in the file could not be parsed as `KEY=VALUE`.
32    ///
33    /// The syntax is described in [`crate::EnvFileSource`]. This error includes the exact
34    /// line number (1-based) and a diagnostic message.
35    #[error("invalid syntax in {path} at line {line}: {message}")]
36    InvalidSyntax {
37        /// Path to the file that caused the error.
38        path: PathBuf,
39        /// 1-based line number where the syntax error occurred.
40        line: usize,
41        /// Description of the syntax problem (e.g. "expected KEY=VALUE, got `...`").
42        message: String,
43    },
44}