1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Errors produced by durable configuration, migration, and encryption operations.
use thiserror::Error;
/// Errors that can occur when interacting with the config store.
#[derive(Debug, Error)]
pub enum ConfigError {
/// Failed to resolve or create the config directory path.
#[error("Config path error: {0}")]
Path(String),
/// Failed to open the database.
#[error("Database open failed: {0}")]
DatabaseOpen(String),
/// Schema migration failed.
#[error("Migration failed: {0}")]
Migration(String),
/// Database query failed.
#[error("Query failed: {0}")]
Query(String),
/// Serialization failed.
#[error("Serialization failed: {0}")]
Serialization(String),
/// Deserialization failed.
#[error("Deserialization failed: {0}")]
Deserialization(String),
/// Record not found.
#[error("Record not found: {0}")]
NotFound(String),
/// Unique constraint violation.
#[error("Conflict: {0}")]
Conflict(String),
/// Credential encryption key not available.
#[error("Credential key unavailable: {0}")]
KeyUnavailable(String),
/// Invalid or malformed credential encryption key.
#[error("Invalid credential key: {0}")]
InvalidKey(String),
/// Encryption failed.
#[error("Encryption failed: {0}")]
Encryption(String),
/// Decryption failed.
#[error("Decryption failed: {0}")]
Decryption(String),
/// Database busy timeout exceeded.
#[error("Database busy: {0}")]
BusyTimeout(String),
/// Input validation failed.
#[error("Validation error: {0}")]
Validation(String),
/// Effective model catalog error.
#[error("Model catalog error: {0}")]
Catalog(String),
/// Automation task references a stored prompt that does not exist.
#[error("Automation task references unknown stored prompt: {0}")]
UnknownStoredPrompt(String),
/// Stored prompt deletion blocked because automation tasks reference it.
#[error("Stored prompt '{prompt_id}' is referenced by automation tasks: {task_ids:?}")]
PromptReferencedByTasks {
/// The prompt that was being deleted.
prompt_id: String,
/// IDs of tasks that reference the prompt.
task_ids: Vec<String>,
},
/// Automation task normalized name collides with an existing task.
#[error(
"Task normalized name '{normalized_name}' collides with existing task '{existing_id}'"
)]
TaskNameConflict {
/// The normalized name that collided.
normalized_name: String,
/// The ID of the existing task that already owns the name.
existing_id: String,
},
/// Schedule references an automation task that does not exist.
#[error("Schedule references unknown automation task: {0}")]
UnknownAutomationTask(String),
/// Automation task deletion blocked because schedules reference it.
#[error("Automation task '{task_id}' is referenced by schedules: {schedule_ids:?}")]
TaskReferencedBySchedules {
/// The task that was being deleted.
task_id: String,
/// IDs of schedules that reference the task.
schedule_ids: Vec<String>,
},
/// Stored prompt normalized name collides with an existing prompt.
#[error(
"Prompt normalized name '{normalized_name}' collides with existing prompt '{existing_id}'"
)]
PromptNameConflict {
/// The normalized name that collided.
normalized_name: String,
/// The ID of the existing prompt that already owns the name.
existing_id: String,
},
/// Profile deletion blocked because stored prompts reference it.
#[error("Profile '{profile_id}' is referenced by prompts: {prompt_ids:?}")]
ProfileReferencedByPrompts {
/// The profile that was being deleted.
profile_id: String,
/// IDs of prompts that reference the profile.
prompt_ids: Vec<String>,
},
/// Deletion cannot proceed because malformed records prevent reference checking.
#[error("Cannot verify referential integrity due to malformed records: {details}")]
IntegrityUnknown {
/// Human-readable details about the unreadable records.
details: String,
},
/// Policy-aware profile deletion would leave fewer valid persisted
/// profiles than the caller requested.
#[error(
"Cannot delete profile: {remaining} valid profile(s) would remain, below the requested minimum of {minimum}"
)]
MinimumValidProfiles {
/// The minimum valid-profile count the caller requested to remain.
minimum: usize,
/// The computed valid-profile count that would remain after deletion.
remaining: usize,
},
}
impl From<sqlx::Error> for ConfigError {
fn from(err: sqlx::Error) -> Self {
match err {
sqlx::Error::RowNotFound => ConfigError::NotFound("record not found".to_string()),
sqlx::Error::Database(db_err) => {
if db_err.message().contains("UNIQUE constraint failed") {
ConfigError::Conflict(db_err.message().to_string())
} else if db_err.message().contains("busy")
|| db_err.message().contains("database is locked")
{
ConfigError::BusyTimeout(db_err.message().to_string())
} else {
ConfigError::Query(db_err.message().to_string())
}
}
_ => ConfigError::Query(err.to_string()),
}
}
}
impl From<std::io::Error> for ConfigError {
fn from(err: std::io::Error) -> Self {
ConfigError::Path(err.to_string())
}
}
impl From<serde_json::Error> for ConfigError {
fn from(err: serde_json::Error) -> Self {
use serde_json::error::Category;
match err.classify() {
Category::Io => ConfigError::Serialization(format!("IO error: {}", err)),
Category::Syntax | Category::Data | Category::Eof => {
ConfigError::Deserialization(err.to_string())
}
}
}
}
impl From<super::effective_catalog::CatalogError> for ConfigError {
fn from(err: super::effective_catalog::CatalogError) -> Self {
ConfigError::Catalog(err.to_string())
}
}