1use serde::Serialize;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum AppError {
6 #[error("{0}")]
7 InvalidInput(String),
8 #[error("{0}")]
9 Auth(String),
10 #[error("{0}")]
11 ReadOnly(String),
12 #[error("{0}")]
13 Permission(String),
14 #[error("{0}")]
15 NotFound(String),
16 #[error("Microsoft Graph throttled the request")]
17 RateLimit(Option<u64>),
18 #[error("{0}")]
19 Api(String),
20 #[error("{0}")]
21 NonInteractive(String),
22 #[error("{0}")]
23 ConfirmationRequired(String),
24 #[error("{0}")]
25 Unconfirmed(String),
26 #[error("{0}")]
27 Unsupported(String),
28 #[error("{0}")]
29 Desktop(String),
30 #[error("{0}")]
31 Unhealthy(String),
32 #[error("{0}")]
33 Io(#[from] std::io::Error),
34 #[error("{0}")]
35 Unexpected(String),
36}
37
38#[derive(Debug, Clone, Copy, Serialize)]
39pub struct ErrorContract {
40 pub kind: &'static str,
41 pub exit_code: i32,
42 pub retryable: bool,
43 pub description: &'static str,
44}
45
46pub const INVALID_INPUT: ErrorContract = ErrorContract {
47 kind: "invalid_input",
48 exit_code: 2,
49 retryable: false,
50 description: "Arguments or local configuration are invalid",
51};
52pub const AUTH: ErrorContract = ErrorContract {
53 kind: "auth",
54 exit_code: 3,
55 retryable: false,
56 description: "Authentication is missing, expired, or rejected",
57};
58pub const PERMISSION: ErrorContract = ErrorContract {
59 kind: "permission_denied",
60 exit_code: 5,
61 retryable: false,
62 description: "The tenant or granted scopes do not permit the operation",
63};
64pub const NOT_FOUND: ErrorContract = ErrorContract {
65 kind: "not_found",
66 exit_code: 4,
67 retryable: false,
68 description: "The requested Teams resource does not exist",
69};
70pub const RATE_LIMIT: ErrorContract = ErrorContract {
71 kind: "rate_limit",
72 exit_code: 6,
73 retryable: true,
74 description: "Microsoft Graph throttled the request",
75};
76pub const API: ErrorContract = ErrorContract {
77 kind: "api_error",
78 exit_code: 5,
79 retryable: false,
80 description: "Microsoft Graph returned an API error",
81};
82pub const NON_INTERACTIVE: ErrorContract = ErrorContract {
83 kind: "tty_required",
84 exit_code: 2,
85 retryable: false,
86 description: "An interactive command was invoked without a terminal",
87};
88pub const CONFIRMATION_REQUIRED: ErrorContract = ErrorContract {
89 kind: "confirmation_required",
90 exit_code: 2,
91 retryable: false,
92 description: "The operation needs confirmation: answer the prompt in a terminal, or pass --yes",
93};
94pub const UNCONFIRMED: ErrorContract = ErrorContract {
95 kind: "unconfirmed",
96 exit_code: 8,
97 retryable: false,
98 description: "A send may have been delivered but was not confirmed; check the conversation before sending again",
99};
100pub const UNEXPECTED: ErrorContract = ErrorContract {
101 kind: "unexpected_error",
102 exit_code: 1,
103 retryable: false,
104 description: "An unexpected local or transport error occurred",
105};
106pub const READ_ONLY: ErrorContract = ErrorContract {
107 kind: "read_only",
108 exit_code: 2,
109 retryable: false,
110 description: "The active profile blocks remote write operations",
111};
112
113pub const UNSUPPORTED: ErrorContract = ErrorContract {
114 kind: "unsupported",
115 exit_code: 2,
116 retryable: false,
117 description: "The selected backend or platform does not support this operation",
118};
119pub const DESKTOP: ErrorContract = ErrorContract {
120 kind: "desktop_error",
121 exit_code: 5,
122 retryable: false,
123 description: "The desktop backend could not complete the request",
124};
125
126pub const UNHEALTHY: ErrorContract = ErrorContract {
127 kind: "unhealthy",
128 exit_code: 7,
129 retryable: false,
130 description: "Diagnostics completed and at least one check failed",
131};
132
133pub const ALL: &[ErrorContract] = &[
134 INVALID_INPUT,
135 AUTH,
136 READ_ONLY,
137 PERMISSION,
138 NOT_FOUND,
139 RATE_LIMIT,
140 API,
141 NON_INTERACTIVE,
142 CONFIRMATION_REQUIRED,
143 UNCONFIRMED,
144 UNEXPECTED,
145 UNSUPPORTED,
146 DESKTOP,
147 UNHEALTHY,
148];
149
150impl AppError {
151 pub fn contract(&self) -> ErrorContract {
152 match self {
153 Self::InvalidInput(_) => INVALID_INPUT,
154 Self::Auth(_) => AUTH,
155 Self::ReadOnly(_) => READ_ONLY,
156 Self::Permission(_) => PERMISSION,
157 Self::NotFound(_) => NOT_FOUND,
158 Self::RateLimit(_) => RATE_LIMIT,
159 Self::Api(_) => API,
160 Self::NonInteractive(_) => NON_INTERACTIVE,
161 Self::ConfirmationRequired(_) => CONFIRMATION_REQUIRED,
162 Self::Unconfirmed(_) => UNCONFIRMED,
163 Self::Unsupported(_) => UNSUPPORTED,
164 Self::Desktop(_) => DESKTOP,
165 Self::Unhealthy(_) => UNHEALTHY,
166 Self::Io(_) | Self::Unexpected(_) => UNEXPECTED,
167 }
168 }
169}