Skip to main content

stackless_core/engine/
error.rs

1//! Engine errors. Substrate failures pass their own stable code
2//! through; the engine adds the step and instance context the §2
3//! contract requires.
4
5use crate::def::DefError;
6use crate::fault::{ErrorContext, Fault, codes};
7use crate::state::StateError;
8use crate::substrate::SubstrateFault;
9
10#[derive(Debug, thiserror::Error)]
11pub enum EngineError {
12    #[error("operation on instance {instance:?} was cancelled; resource records were retained")]
13    Cancelled { instance: String },
14    #[error(transparent)]
15    Def(#[from] DefError),
16
17    #[error(transparent)]
18    State(#[from] StateError),
19
20    #[error("instance {instance:?} lives on substrate {existing:?}, not {requested:?}")]
21    SubstrateMismatch {
22        instance: String,
23        existing: String,
24        requested: String,
25    },
26
27    #[error("substrate {substrate:?} does not run pinned checkouts (--source)")]
28    SourceOverrideUnsupported { substrate: String },
29
30    #[error(
31        "service {service:?} --source path {path:?} is already pinned by active instance {other:?}"
32    )]
33    SourceOverrideShared {
34        instance: String,
35        service: String,
36        path: String,
37        other: String,
38    },
39
40    #[error("step {step} on instance {instance:?} failed: {fault}")]
41    Step {
42        instance: String,
43        step: String,
44        fault: SubstrateFault,
45    },
46
47    #[error("substrate {substrate} rejected the definition: {fault}")]
48    SubstrateValidation {
49        substrate: String,
50        fault: SubstrateFault,
51    },
52
53    #[error("teardown of {instance:?} left survivors: {survivors:?}; failures: {failures:?}")]
54    TeardownSurvivors {
55        instance: String,
56        survivors: Vec<String>,
57        failures: Vec<String>,
58    },
59}
60
61impl Fault for EngineError {
62    fn code(&self) -> &str {
63        match self {
64            Self::Cancelled { .. } => codes::OPERATION_CANCELLED,
65            Self::Def(err) => err.code(),
66            Self::State(err) => err.code(),
67            Self::SubstrateMismatch { .. } => codes::ENGINE_SUBSTRATE_MISMATCH,
68            Self::SourceOverrideUnsupported { .. } => codes::ENGINE_SOURCE_OVERRIDE_UNSUPPORTED,
69            Self::SourceOverrideShared { .. } => codes::ENGINE_SOURCE_OVERRIDE_SHARED,
70            // The substrate's own code is the meaningful one; the
71            // engine only adds context.
72            Self::Step { fault, .. } | Self::SubstrateValidation { fault, .. } => &fault.code,
73            Self::TeardownSurvivors { .. } => codes::ENGINE_TEARDOWN_SURVIVORS,
74        }
75    }
76
77    fn remediation(&self) -> String {
78        match self {
79            Self::Cancelled { .. } => {
80                "run up to resume or down to remove the retained resources".into()
81            }
82            Self::Def(err) => err.remediation(),
83            Self::State(err) => err.remediation(),
84            Self::SubstrateMismatch {
85                instance, existing, ..
86            } => format!(
87                "the substrate is chosen at creation only: operate on {instance:?} without \
88                 --on (it resolves to {existing:?}), or pick a new name for a {existing:?}-free \
89                 instance"
90            ),
91            Self::SourceOverrideUnsupported { .. } => {
92                "commit and push, then pin the ref in stackless.toml — cloud substrates deploy \
93                 committed refs only"
94                    .into()
95            }
96            Self::SourceOverrideShared { other, .. } => format!(
97                "use a separate git worktree per parallel agent, omit --source so stackless \
98                 materializes per-instance checkouts, or `stackless down {other}` before reusing \
99                 this checkout"
100            ),
101            Self::Step { fault, .. } | Self::SubstrateValidation { fault, .. } => {
102                fault.remediation.clone()
103            }
104            Self::TeardownSurvivors { survivors, .. } => format!(
105                "these resources still exist and may bill or hold state: {survivors:?}; re-run \
106                 `stackless down` to retry, or remove them on the substrate and re-run to verify"
107            ),
108        }
109    }
110
111    fn step(&self) -> Option<&str> {
112        match self {
113            Self::Step { step, .. } => Some(step),
114            _ => None,
115        }
116    }
117
118    fn instance(&self) -> Option<&str> {
119        match self {
120            Self::State(err) => err.instance(),
121            Self::Cancelled { instance }
122            | Self::SubstrateMismatch { instance, .. }
123            | Self::SourceOverrideShared { instance, .. }
124            | Self::Step { instance, .. }
125            | Self::TeardownSurvivors { instance, .. } => Some(instance),
126            _ => None,
127        }
128    }
129
130    fn context(&self) -> ErrorContext {
131        match self {
132            Self::Step { fault, .. } | Self::SubstrateValidation { fault, .. } => {
133                fault.context.as_ref().clone()
134            }
135            _ => ErrorContext::default(),
136        }
137    }
138}