stackless_core/state/
error.rs1use crate::fault::{Fault, codes};
4
5#[derive(Debug, thiserror::Error)]
6pub enum StateError {
7 #[error("state connection mutex poisoned")]
8 Poisoned,
9
10 #[error("resource journal invariant failed: {detail}")]
11 ResourceInvariant { detail: String },
12
13 #[error("{instance}: {node} still has receipts on {existing}; cannot move it to {requested}")]
14 PlacementConflict {
15 instance: String,
16 node: String,
17 existing: String,
18 requested: String,
19 },
20
21 #[error("cannot open state store at {path}: {source}")]
22 Open {
23 path: String,
24 source: rusqlite::Error,
25 },
26
27 #[error("cannot create state directory {path}: {source}")]
28 StateDir {
29 path: String,
30 source: std::io::Error,
31 },
32
33 #[error("cannot create or protect state file {path}: {source}")]
34 StateFile {
35 path: String,
36 source: std::io::Error,
37 },
38
39 #[error("state store migration failed: {source}")]
40 Migrate { source: rusqlite::Error },
41
42 #[error("state store query failed: {source}")]
43 Query {
44 #[from]
45 source: rusqlite::Error,
46 },
47
48 #[error("instance {name:?} already exists on substrate {existing_substrate:?}")]
49 InstanceExists {
50 name: String,
51 existing_substrate: String,
52 },
53
54 #[error("no instance named {name:?}")]
55 InstanceNotFound { name: String },
56
57 #[error(
58 "instance {instance:?} is locked by operation {operation:?} (pid {holder_pid}, started {acquired_at})"
59 )]
60 LockHeld {
61 instance: String,
62 operation: String,
63 holder_pid: u32,
64 acquired_at: i64,
65 },
66
67 #[error("direct remote state databases are disabled; select one controller")]
68 RemoteDisabled,
69
70 #[error("state row decode failed at column {column}: {detail}")]
71 RowDecode { column: usize, detail: String },
72}
73
74impl StateError {
75 pub(super) fn row_range(column: usize) -> Self {
76 Self::RowDecode {
77 column,
78 detail: "column index out of range".into(),
79 }
80 }
81 pub(super) fn row_type(column: usize, want: &str) -> Self {
82 Self::RowDecode {
83 column,
84 detail: format!("value is not a {want}"),
85 }
86 }
87}
88
89impl Fault for StateError {
90 fn code(&self) -> &str {
91 match self {
92 Self::Open { .. } | Self::StateDir { .. } | Self::StateFile { .. } => codes::STATE_OPEN,
93 Self::Migrate { .. } => codes::STATE_MIGRATE,
94 Self::Query { .. } | Self::Poisoned | Self::ResourceInvariant { .. } => {
95 codes::STATE_QUERY
96 }
97 Self::PlacementConflict { .. } => codes::STATE_PLACEMENT_CONFLICT,
98 Self::InstanceExists { .. } => codes::STATE_INSTANCE_EXISTS,
99 Self::InstanceNotFound { .. } => codes::STATE_INSTANCE_NOT_FOUND,
100 Self::LockHeld { .. } => codes::STATE_LOCK_HELD,
101 Self::RemoteDisabled => codes::STATE_REMOTE_DISABLED,
102 Self::RowDecode { .. } => codes::STATE_ROW_DECODE,
103 }
104 }
105
106 fn remediation(&self) -> String {
107 match self {
108 Self::Open { path, .. }
109 | Self::StateDir { path, .. }
110 | Self::StateFile { path, .. } => format!(
111 "check that {path} is writable; set XDG_STATE_HOME to relocate the state dir"
112 ),
113 Self::Migrate { .. } => {
114 "keep the state file intact; check the schema version and export completeness before upgrading or migrating it"
115 .into()
116 }
117 Self::Poisoned => {
118 "restart the controller; inspect the preceding panic before retrying".into()
119 }
120 Self::PlacementConflict { .. } => "retire the workload or resource and verify its teardown before placing a new lifetime on another provider".into(),
121 Self::ResourceInvariant { .. } => {
122 "inspect the resource inventory; do not discard unresolved resource records".into()
123 }
124 Self::Query { .. } => {
125 "re-run the command; if it persists, the state file may be corrupt — move it \
126 aside and re-adopt instances with `stackless up`"
127 .into()
128 }
129 Self::InstanceExists {
130 name,
131 existing_substrate,
132 } => format!(
133 "names are unique across substrates: `stackless up --name {name}` resumes the \
134 existing {existing_substrate} instance, or pick a different name"
135 ),
136 Self::InstanceNotFound { name } => format!(
137 "`stackless list` shows known instances; `stackless up --name {name}` creates it"
138 ),
139 Self::LockHeld { instance, .. } => format!(
140 "wait for the running operation on {instance:?} to finish and retry; if the \
141 holder crashed it will be taken over automatically on the next attempt"
142 ),
143 Self::RemoteDisabled => {
144 "use STACKLESS_CONTROLLER=ssh://host and unset STACKLESS_STATE_URL and STACKLESS_STATE_TOKEN; migrate legacy database exports before starting the controller".into()
145 }
146 Self::RowDecode { .. } => {
147 "the state store returned an unexpected row shape; the state file may be from a \
148 newer stackless — upgrade stackless"
149 .into()
150 }
151 }
152 }
153
154 fn instance(&self) -> Option<&str> {
155 match self {
156 Self::InstanceExists { name, .. } | Self::InstanceNotFound { name } => Some(name),
157 Self::LockHeld { instance, .. } | Self::PlacementConflict { instance, .. } => {
158 Some(instance)
159 }
160 _ => None,
161 }
162 }
163}