rightkit_document_workspace/
error.rs1use rightkit_db::rusqlite;
4use serde::Serialize;
5
6#[derive(Debug, Serialize, Clone, PartialEq, Eq)]
9#[serde(tag = "code", content = "message", rename_all = "snake_case")]
10pub enum WorkspaceError {
11 BrokerDenied(String),
13 FileNotFound(String),
15 PermissionDenied(String),
17 UnsupportedFormat(String),
19 DocumentTooLarge(String),
21 Io(String),
23 Encoding(String),
25 WriteConflict(String),
27 Dialog(String),
29 Watch(String),
31 Persistence(String),
33 Other(String),
35}
36
37impl std::fmt::Display for WorkspaceError {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 match self {
40 Self::BrokerDenied(m) => write!(f, "access denied: {m}"),
41 Self::FileNotFound(m) => write!(f, "file not found: {m}"),
42 Self::PermissionDenied(m) => write!(f, "permission denied: {m}"),
43 Self::UnsupportedFormat(m) => write!(f, "unsupported format: {m}"),
44 Self::DocumentTooLarge(m) => write!(f, "document too large: {m}"),
45 Self::Io(m) => write!(f, "I/O error: {m}"),
46 Self::Encoding(m) => write!(f, "encoding error: {m}"),
47 Self::WriteConflict(m) => write!(f, "write conflict: {m}"),
48 Self::Dialog(m) => write!(f, "dialog error: {m}"),
49 Self::Watch(m) => write!(f, "watch error: {m}"),
50 Self::Persistence(m) => write!(f, "storage error: {m}"),
51 Self::Other(m) => write!(f, "{m}"),
52 }
53 }
54}
55
56impl std::error::Error for WorkspaceError {}
57
58impl From<String> for WorkspaceError {
59 fn from(s: String) -> Self {
60 Self::Other(s)
61 }
62}
63
64impl From<&str> for WorkspaceError {
65 fn from(s: &str) -> Self {
66 Self::Other(s.to_string())
67 }
68}
69
70impl From<std::io::Error> for WorkspaceError {
71 fn from(e: std::io::Error) -> Self {
72 Self::Io(e.to_string())
73 }
74}
75
76impl From<rusqlite::Error> for WorkspaceError {
77 fn from(e: rusqlite::Error) -> Self {
78 Self::Persistence(e.to_string())
79 }
80}
81
82pub fn broker_error(message: String) -> WorkspaceError {
87 let lower = message.to_ascii_lowercase();
88 if lower.contains("not authorized") || lower.contains("not just written") {
89 WorkspaceError::BrokerDenied(message)
90 } else if lower.contains("no such file")
91 || lower.contains("cannot find the path")
92 || lower.contains("cannot find the file")
93 || lower.contains("not a file")
94 {
95 WorkspaceError::FileNotFound(message)
96 } else {
97 WorkspaceError::Other(message)
98 }
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn broker_error_classifies_by_message() {
107 assert!(matches!(
108 broker_error("path is not authorized".into()),
109 WorkspaceError::BrokerDenied(_)
110 ));
111 assert!(matches!(
112 broker_error("No such file or directory".into()),
113 WorkspaceError::FileNotFound(_)
114 ));
115 assert!(matches!(
116 broker_error("boom".into()),
117 WorkspaceError::Other(_)
118 ));
119 }
120
121 #[test]
122 fn serializes_with_code_and_message() {
123 let json = serde_json::to_string(&WorkspaceError::Io("x".into())).unwrap();
124 assert_eq!(json, r#"{"code":"io","message":"x"}"#);
125 }
126}