ferro_projection/
error.rs1#[derive(Debug, thiserror::Error)]
8pub enum ProjectionError {
9 #[error("projection: db error: {0}")]
11 Db(#[from] sea_orm::DbErr),
12
13 #[error("projection: json error: {0}")]
16 Json(#[from] serde_json::Error),
17
18 #[error("projection: broadcast error: {0}")]
28 Broadcast(String),
29
30 #[error("projection: events error: {0}")]
33 Events(String),
34
35 #[error("projection: state not found for {name}/{key}")]
39 StateNotFound { name: &'static str, key: String },
40}
41
42impl From<ferro_broadcast::Error> for ProjectionError {
43 fn from(e: ferro_broadcast::Error) -> Self {
44 Self::Broadcast(e.to_string())
45 }
46}
47
48impl From<ferro_events::Error> for ProjectionError {
49 fn from(e: ferro_events::Error) -> Self {
50 Self::Events(e.to_string())
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn db_from_sea_orm_dberr() {
60 let db_err = sea_orm::DbErr::Custom("test".into());
61 let e: ProjectionError = ProjectionError::from(db_err);
62 assert!(matches!(e, ProjectionError::Db(_)));
63 assert!(e.to_string().starts_with("projection: db error: "));
64 }
65
66 #[test]
67 fn json_from_serde_json_error() {
68 let j: serde_json::Error =
69 serde_json::from_str::<serde_json::Value>("not json").unwrap_err();
70 let e: ProjectionError = ProjectionError::from(j);
71 assert!(matches!(e, ProjectionError::Json(_)));
72 assert!(e.to_string().starts_with("projection: json error: "));
73 }
74
75 #[test]
76 fn broadcast_display() {
77 let e = ProjectionError::Broadcast("oops".into());
78 assert_eq!(e.to_string(), "projection: broadcast error: oops");
79 }
80
81 #[test]
82 fn events_display() {
83 let e = ProjectionError::Events("oops".into());
84 assert_eq!(e.to_string(), "projection: events error: oops");
85 }
86
87 #[test]
88 fn state_not_found_display() {
89 let e = ProjectionError::StateNotFound {
90 name: "inventory.dashboard",
91 key: "warehouse-a".into(),
92 };
93 assert_eq!(
94 e.to_string(),
95 "projection: state not found for inventory.dashboard/warehouse-a"
96 );
97 }
98}