1use thiserror::Error;
7use uuid::Uuid;
8
9use crate::entities::RunStatus;
10
11#[derive(Debug, Error)]
23pub enum StoreError {
24 #[error("run not found: {0}")]
26 RunNotFound(Uuid),
27
28 #[error("step not found: {0}")]
30 StepNotFound(Uuid),
31
32 #[error("invalid status transition: {from} -> {to}")]
34 InvalidTransition {
35 from: RunStatus,
37 to: RunStatus,
39 },
40
41 #[error("email already exists: {0}")]
43 DuplicateEmail(String),
44
45 #[error("username already exists: {0}")]
47 DuplicateUsername(String),
48
49 #[error("user not found: {0}")]
51 UserNotFound(Uuid),
52
53 #[error("lease lost on run {run_id}")]
58 LeaseLost {
59 run_id: Uuid,
61 held_by: Option<String>,
63 },
64
65 #[error("artifact {name:?} already exists on step {step_id}")]
67 DuplicateArtifact {
68 step_id: Uuid,
70 name: String,
72 },
73
74 #[error("schedule not found: {0}")]
76 ScheduleNotFound(Uuid),
77
78 #[error("database error: {0}")]
80 Database(String),
81
82 #[error("serialization error: {0}")]
84 Serialization(#[from] serde_json::Error),
85
86 #[error("crypto error: {0}")]
88 Crypto(String),
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn run_not_found_display() {
97 let id = Uuid::nil();
98 let err = StoreError::RunNotFound(id);
99 assert_eq!(err.to_string(), format!("run not found: {id}"));
100 }
101
102 #[test]
103 fn step_not_found_display() {
104 let id = Uuid::nil();
105 let err = StoreError::StepNotFound(id);
106 assert_eq!(err.to_string(), format!("step not found: {id}"));
107 }
108
109 #[test]
110 fn invalid_transition_display() {
111 let err = StoreError::InvalidTransition {
112 from: RunStatus::Pending,
113 to: RunStatus::Completed,
114 };
115 assert!(err.to_string().contains("Pending"));
116 assert!(err.to_string().contains("Completed"));
117 }
118
119 #[test]
120 fn database_error_display() {
121 let err = StoreError::Database("connection refused".to_string());
122 assert!(err.to_string().contains("connection refused"));
123 }
124
125 #[test]
126 fn serialization_error_from_serde() {
127 let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
128 let err = StoreError::from(serde_err);
129 assert!(err.to_string().contains("serialization error"));
130 }
131}