Skip to main content

rightkit_document_workspace/
error.rs

1//! Typed error envelope for workspace command failures.
2
3use rightkit_db::rusqlite;
4use serde::Serialize;
5
6/// Typed error codes for frontend-facing command failures. The serialized shape
7/// (`{"code": "...", "message": "..."}`) is the contract the renderer classifies on.
8#[derive(Debug, Serialize, Clone, PartialEq, Eq)]
9#[serde(tag = "code", content = "message", rename_all = "snake_case")]
10pub enum WorkspaceError {
11    /// The path-grant broker denied access to the requested path.
12    BrokerDenied(String),
13    /// The file was not found on disk.
14    FileNotFound(String),
15    /// The OS denied access to a location. The message is user-facing.
16    PermissionDenied(String),
17    /// The file type is not supported.
18    UnsupportedFormat(String),
19    /// The document is too large to open or save safely.
20    DocumentTooLarge(String),
21    /// An I/O error occurred.
22    Io(String),
23    /// The encoding could not be determined or applied.
24    Encoding(String),
25    /// The source changed after it was opened and before an in-place save committed.
26    WriteConflict(String),
27    /// A dialog operation was cancelled or failed.
28    Dialog(String),
29    /// The filesystem watcher could not be armed or torn down.
30    Watch(String),
31    /// The local SQLite persistence layer failed (query, migration, or lock).
32    Persistence(String),
33    /// A generic error that does not fit the other categories.
34    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
82/// Classify a broker or document string error into the right envelope variant, preserving the
83/// exact message text. Grant and document backends return `Result<_, String>`, so command sites
84/// call this at the `map_err` boundary instead of relying on the blanket `From<String>`, which
85/// would flatten every broker rejection to `Other` and lose the not-found/denied distinction.
86pub 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}