1use serde::Serialize;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum MacflyError {
6 #[error("macOS permission denied (TCC) for {target}: {detail}")]
7 TccPermissionDenied { target: String, detail: String },
8
9 #[error("operation blocked by profile policy: {0}")]
10 ScopeDenied(String),
11
12 #[error("resource not found: {0}")]
13 NotFound(String),
14
15 #[error("backend timed out after {seconds}s")]
16 Timeout { seconds: u64 },
17
18 #[error("script execution failed: {0}")]
19 ScriptError(String),
20
21 #[error("invalid configuration at {path}: {detail}")]
22 ConfigError { path: String, detail: String },
23
24 #[error("plan error: {0}")]
25 PlanError(String),
26
27 #[error("plan approval required or invalid: {0}")]
28 ApprovalError(String),
29
30 #[error("serialization error: {0}")]
31 Serialization(#[from] serde_json::Error),
32
33 #[error("io error: {0}")]
34 Io(#[from] std::io::Error),
35}
36
37#[derive(Debug, Serialize)]
38pub struct ErrorEnvelope {
39 pub ok: bool,
40 pub error: ErrorBody,
41}
42
43#[derive(Debug, Serialize)]
44pub struct ErrorBody {
45 pub code: &'static str,
46 pub message: String,
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub hint: Option<String>,
49}
50
51impl MacflyError {
52 pub fn code(&self) -> &'static str {
53 match self {
54 Self::TccPermissionDenied { .. } => "TCC_PERMISSION_DENIED",
55 Self::ScopeDenied(_) => "SCOPE_DENIED",
56 Self::NotFound(_) => "NOT_FOUND",
57 Self::Timeout { .. } => "TIMEOUT",
58 Self::ScriptError(_) => "SCRIPT_ERROR",
59 Self::ConfigError { .. } => "CONFIG_ERROR",
60 Self::PlanError(_) => "PLAN_ERROR",
61 Self::ApprovalError(_) => "APPROVAL_ERROR",
62 Self::Serialization(_) => "SERIALIZATION_ERROR",
63 Self::Io(_) => "IO_ERROR",
64 }
65 }
66
67 pub fn hint(&self) -> Option<String> {
68 match self {
69 Self::TccPermissionDenied { target, .. } => Some(format!(
70 "Erlaube deinem Terminal den Automationszugriff auf {target} unter Systemeinstellungen → Datenschutz & Sicherheit → Automation, und prüfe danach `macfly doctor`."
71 )),
72 Self::Timeout { .. } => Some(
73 "Erhöhe ggf. `script_timeout_secs` in der Config oder begrenze die Abfrage mit `--folder` / `--limit`."
74 .to_string(),
75 ),
76 _ => None,
77 }
78 }
79
80 pub fn to_envelope(&self) -> ErrorEnvelope {
81 ErrorEnvelope {
82 ok: false,
83 error: ErrorBody {
84 code: self.code(),
85 message: self.to_string(),
86 hint: self.hint(),
87 },
88 }
89 }
90}