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//!
14//! Copyright (c) systemprompt.io — Business Source License 1.1.
15//! See <https://systemprompt.io> for licensing details.
16
17use std::path::PathBuf;
18
19use systemprompt_models::errors::SecretsError;
20use systemprompt_models::profile::{GatewayProfileError, ProfileError};
21
22use crate::bootstrap::{ProfileBootstrapError, SecretsBootstrapError};
23use crate::services::ConfigValidationError;
24
25pub type ConfigResult<T> = Result<T, ConfigError>;
26
27#[derive(Debug, thiserror::Error)]
28#[non_exhaustive]
29pub enum ConfigError {
30    #[error("Config already initialized")]
31    AlreadyInitialized,
32
33    #[error(transparent)]
34    Profile(#[from] ProfileBootstrapError),
35
36    #[error(transparent)]
37    Secrets(#[from] SecretsBootstrapError),
38
39    #[error(transparent)]
40    ProfileParse(#[from] ProfileError),
41
42    #[error(transparent)]
43    Gateway(#[from] GatewayProfileError),
44
45    #[error(transparent)]
46    SchemaValidation(#[from] ConfigValidationError),
47
48    #[error(transparent)]
49    SecretsParse(#[from] SecretsError),
50
51    #[error(transparent)]
52    Io(#[from] std::io::Error),
53
54    #[error(transparent)]
55    Json(#[from] serde_json::Error),
56
57    #[error(transparent)]
58    Yaml(#[from] serde_yaml::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(
105        "Profile is missing required `system_admin.username` and `SYSTEMPROMPT_SYSTEM_ADMIN` is \
106         not set. The platform refuses to start without an explicit system-admin identity."
107    )]
108    MissingSystemAdmin,
109
110    #[error("No provider named {name}")]
111    ProviderNotFound { name: String },
112
113    #[error("No model with id {id} under provider {provider}")]
114    ModelNotFound { id: String, provider: String },
115
116    #[error("Access token expiry must be positive")]
117    NonPositiveAccessTokenExpiry,
118
119    #[error("Refresh token expiry must be positive")]
120    NonPositiveRefreshTokenExpiry,
121
122    #[error("No trusted issuer found with issuer {issuer}")]
123    TrustedIssuerNotFound { issuer: String },
124
125    #[error("{message}")]
126    Other { message: String },
127}
128
129impl ConfigError {
130    pub fn other(message: impl Into<String>) -> Self {
131        Self::Other {
132            message: message.into(),
133        }
134    }
135}