Skip to main content

stackless_core/def/
error.rs

1//! Definition-layer errors. Every variant carries a stable code and a
2//! remediation (ARCHITECTURE.md §2, §8).
3
4use crate::fault::{Fault, codes};
5
6#[derive(Debug, thiserror::Error)]
7pub enum DefError {
8    #[error("stackless.toml is not valid TOML: {message}")]
9    Syntax { message: String },
10
11    #[error("stackless.toml does not match the schema: {message}")]
12    Schema { message: String },
13
14    #[error("{kind} name {name:?} is not DNS-safe")]
15    NameInvalid { kind: &'static str, name: String },
16
17    #[error("the stack declares no workloads or resources")]
18    NoServices,
19
20    #[error("unknown key {key:?} under {location}")]
21    UnknownKey {
22        location: String,
23        key: String,
24        known_substrates: Vec<String>,
25    },
26
27    #[error("`depends_on` under {location} is not part of the schema")]
28    DependsOnRejected { location: String },
29
30    #[error("substrate block {location} must be a table, found {found}")]
31    SubstrateBlockInvalid { location: String, found: String },
32
33    #[error("service {service:?} has no [services.{service}.{substrate}] config")]
34    SubstrateConfigMissing { service: String, substrate: String },
35
36    #[error("multiple services declare root_origin: {services:?}")]
37    RootOriginConflict { services: Vec<String> },
38
39    #[error("invalid interpolation reference {reference:?} in {location}: {detail}")]
40    ReferenceSyntax {
41        location: String,
42        reference: String,
43        detail: String,
44    },
45
46    #[error("{location} references undeclared {kind} {name:?}")]
47    UndeclaredReference {
48        location: String,
49        kind: &'static str,
50        name: String,
51    },
52
53    #[error("{location} references endpoint {name:?}, whose URL is not available yet")]
54    EndpointUnavailable { location: String, name: String },
55
56    #[error("{location} uses secret {key:?} which is not in [secrets].required")]
57    SecretNotRequired { location: String, key: String },
58
59    #[error("[integrations.{integration}] is invalid: {detail}")]
60    IntegrationInvalid { integration: String, detail: String },
61
62    #[error("the wiring graph has a dependency cycle through: {nodes}")]
63    WiringCycle { nodes: String },
64
65    #[error("env under {location} must be a table of string values")]
66    EnvNotStrings { location: String },
67}
68
69impl Fault for DefError {
70    fn code(&self) -> &str {
71        match self {
72            Self::Syntax { .. } => codes::DEF_PARSE_SYNTAX,
73            Self::Schema { .. } => codes::DEF_PARSE_SCHEMA,
74            Self::NameInvalid { .. } => codes::DEF_NAME_INVALID,
75            Self::NoServices => codes::DEF_NO_SERVICES,
76            Self::UnknownKey { .. } => codes::DEF_UNKNOWN_KEY,
77            Self::DependsOnRejected { .. } => codes::DEF_DEPENDS_ON_REJECTED,
78            Self::SubstrateBlockInvalid { .. } => codes::DEF_SUBSTRATE_BLOCK_INVALID,
79            Self::SubstrateConfigMissing { .. } => codes::DEF_SUBSTRATE_CONFIG_MISSING,
80            Self::RootOriginConflict { .. } => codes::DEF_ROOT_ORIGIN_CONFLICT,
81            Self::ReferenceSyntax { .. } => codes::DEF_REFERENCE_SYNTAX,
82            Self::UndeclaredReference { .. } => codes::DEF_UNDECLARED_REFERENCE,
83            Self::EndpointUnavailable { .. } => codes::DEF_ENDPOINT_UNAVAILABLE,
84            Self::SecretNotRequired { .. } => codes::DEF_SECRET_NOT_REQUIRED,
85            Self::IntegrationInvalid { .. } => codes::DEF_INTEGRATION_INVALID,
86            Self::WiringCycle { .. } => codes::DEF_WIRING_CYCLE,
87            Self::EnvNotStrings { .. } => codes::DEF_ENV_NOT_STRINGS,
88        }
89    }
90
91    fn remediation(&self) -> String {
92        match self {
93            Self::Syntax { .. } => "fix the TOML syntax at the location shown, then re-run".into(),
94            Self::Schema { .. } => "compare the failing key against docs/SCHEMA.md; \
95                 remove or rename keys the schema does not define"
96                .into(),
97            Self::NameInvalid { kind, .. } => format!(
98                "rename the {kind} to lowercase letters, digits, and hyphens, starting with a \
99                 letter (it becomes hostnames and cloud service names)"
100            ),
101            Self::NoServices => "declare a workload, job, or resource in stackless.toml".into(),
102            Self::UnknownKey {
103                key,
104                known_substrates,
105                ..
106            } => format!(
107                "{key:?} is neither a schema field nor a registered substrate; known substrates \
108                 are {known_substrates:?} — fix the typo or remove the key"
109            ),
110            Self::DependsOnRejected { .. } => {
111                "express the dependency in wiring instead: reference the dependency from this \
112                 service's env (e.g. CORS_ALLOWED_ORIGINS = \"${services.web.origin}\"); the \
113                 dependency graph is derived from wiring, never declared separately"
114                    .into()
115            }
116            Self::SubstrateBlockInvalid { location, .. } => {
117                format!("make {location} a TOML table, e.g. [{location}]")
118            }
119            Self::SubstrateConfigMissing { service, substrate } => format!(
120                "add a [services.{service}.{substrate}] block with the config that substrate \
121                 requires, or bring the instance up on a substrate this service supports"
122            ),
123            Self::RootOriginConflict { services } => format!(
124                "keep root_origin = true on exactly one of {services:?} and remove it from the \
125                 others"
126            ),
127            Self::ReferenceSyntax { .. } => {
128                "valid references are ${stack.name}, ${instance.name}, \
129                 ${services.<name>.origin}, ${endpoints.<name>.url}, ${secrets.<KEY>}, and \
130                 ${integrations.<name>.<output>}"
131                    .into()
132            }
133            Self::UndeclaredReference { kind, name, .. } => format!(
134                "declare [{kind}s.{name}] in stackless.toml, or fix the reference to name a \
135                 declared {kind}"
136            ),
137            Self::EndpointUnavailable { name, .. } => format!(
138                "start the workload for endpoints.{name}, or declare its caller-managed URL"
139            ),
140            Self::SecretNotRequired { key, .. } => format!(
141                "add {key:?} to [secrets].required so it is resolved and validated before \
142                 anything provisions"
143            ),
144            Self::IntegrationInvalid { integration, .. } => format!(
145                "fix [integrations.{integration}]; declare provider and provider-specific \
146                 config (see `stackless check` / SCHEMA.md)"
147            ),
148            Self::WiringCycle { .. } => {
149                "break the cycle: at least one of these references must be removed or replaced \
150                 with one that does not require its target to start first"
151                    .into()
152            }
153            Self::EnvNotStrings { location } => {
154                format!("every value under {location} must be a TOML string")
155            }
156        }
157    }
158}