Skip to main content

systemprompt_config/
error.rs

1//! Public error type for `systemprompt-config`.
2//!
3//! All public APIs of this crate return [`ConfigError`] (or
4//! [`ConfigResult<T>`]) instead of `anyhow::Error`. The enum is
5//! `#[non_exhaustive]` so additional variants can be added in patch
6//! releases without breaking downstream code that performs exhaustive
7//! matching only on the documented variants.
8//!
9//! Upstream errors are composed via `#[from]` so callers can use `?`
10//! transparently from `std::io`, `serde_json`, `serde_yaml`, and
11//! `regex` operations performed inside the bootstrap and validator
12//! pipelines.
13
14use std::path::PathBuf;
15
16use systemprompt_models::errors::SecretsError;
17use systemprompt_models::profile::{GatewayProfileError, ProfileError};
18
19use crate::bootstrap::{ProfileBootstrapError, SecretsBootstrapError};
20use crate::services::ConfigValidationError;
21
22pub type ConfigResult<T> = Result<T, ConfigError>;
23
24#[derive(Debug, thiserror::Error)]
25#[non_exhaustive]
26pub enum ConfigError {
27    #[error("Config already initialized")]
28    AlreadyInitialized,
29
30    #[error(transparent)]
31    Profile(#[from] ProfileBootstrapError),
32
33    #[error(transparent)]
34    Secrets(#[from] SecretsBootstrapError),
35
36    #[error(transparent)]
37    ProfileParse(#[from] ProfileError),
38
39    #[error(transparent)]
40    Gateway(#[from] GatewayProfileError),
41
42    #[error(transparent)]
43    SchemaValidation(#[from] ConfigValidationError),
44
45    #[error(transparent)]
46    SecretsParse(#[from] SecretsError),
47
48    #[error(transparent)]
49    Io(#[from] std::io::Error),
50
51    #[error(transparent)]
52    Json(#[from] serde_json::Error),
53
54    #[error(transparent)]
55    Yaml(#[from] serde_yaml::Error),
56
57    #[error(transparent)]
58    Regex(#[from] regex::Error),
59
60    #[error("Missing required path: paths.{field}")]
61    MissingProfilePath { field: String },
62
63    #[error("Failed to canonicalize {name} path: {source}")]
64    CanonicalizePath {
65        name: String,
66        #[source]
67        source: std::io::Error,
68    },
69
70    #[error("Profile path '{field}' cannot be read: {path}")]
71    ReadProfilePath {
72        field: String,
73        path: PathBuf,
74        #[source]
75        source: std::io::Error,
76    },
77
78    #[error("Profile path '{field}' has invalid YAML: {path}")]
79    InvalidProfileYaml {
80        field: String,
81        path: PathBuf,
82        #[source]
83        source: serde_yaml::Error,
84    },
85
86    #[error("Profile path validation failed: {message}")]
87    ProfilePathReport { message: String },
88
89    #[error("Unsupported database type '{db_type}'. Only 'postgres' is supported.")]
90    UnsupportedDatabaseType { db_type: String },
91
92    #[error("Invalid database URL: {message}")]
93    InvalidDatabaseUrl { message: String },
94
95    #[error("Failed to resolve variables after {passes} passes: {unresolved}")]
96    UnresolvedVariables { passes: usize, unresolved: String },
97
98    #[error("{count} validation error(s)")]
99    ValidationErrors { count: usize },
100
101    #[error("Required config file missing: {path}")]
102    EnvironmentConfigMissing { path: PathBuf },
103
104    #[error("Regex capture group {index} missing")]
105    MissingCaptureGroup { index: usize },
106
107    #[error("{message}")]
108    Other { message: String },
109}
110
111impl ConfigError {
112    pub fn other(message: impl Into<String>) -> Self {
113        Self::Other {
114            message: message.into(),
115        }
116    }
117}