1#[derive(Debug, thiserror::Error)]
2pub enum ClickUpError {
3 #[error("HTTP error: {0}")]
4 Http(#[from] reqwest::Error),
5
6 #[error("Authentication error: {0}")]
7 Auth(String),
8
9 #[error("API error {status}{}: {message}", code.as_deref().map(|c| format!(" [{c}]")).unwrap_or_default())]
10 Api {
11 status: u16,
12 code: Option<String>,
13 message: String,
14 trace_id: Option<String>,
15 },
16
17 #[error("Not found: {0}")]
18 NotFound(String),
19
20 #[error("Rate limited until {reset_at}")]
21 RateLimit { reset_at: i64 },
22
23 #[error("Configuration error: {0}")]
24 Config(String),
25
26 #[error("Could not resolve {kind} '{input}'{}", suggestion_hint(.suggestions))]
27 Resolve {
28 kind: &'static str,
29 input: String,
30 suggestions: Vec<String>,
31 },
32
33 #[error("Invalid input: {0}")]
34 Invalid(String),
35
36 #[error("Serialization error: {0}")]
37 Serialization(#[from] serde_json::Error),
38
39 #[error("IO error: {0}")]
40 Io(#[from] std::io::Error),
41}
42
43pub type Result<T> = std::result::Result<T, ClickUpError>;
44
45fn suggestion_hint(suggestions: &[String]) -> String {
46 if suggestions.is_empty() {
47 String::new()
48 } else {
49 format!(" (did you mean: {}?)", suggestions.join(", "))
50 }
51}
52
53impl ClickUpError {
54 pub fn resolve(kind: &'static str, input: impl Into<String>, suggestions: Vec<String>) -> Self {
55 Self::Resolve {
56 kind,
57 input: input.into(),
58 suggestions,
59 }
60 }
61}
62
63pub fn redact(text: &str) -> String {
65 let mut out = String::with_capacity(text.len());
66 let mut rest = text;
67 while let Some(pos) = rest.find("pk_") {
68 out.push_str(&rest[..pos]);
69 out.push_str("pk_***");
70 let tail = &rest[pos + 3..];
71 let end = tail
72 .find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
73 .unwrap_or(tail.len());
74 rest = &tail[end..];
75 }
76 out.push_str(rest);
77 out
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn redacts_tokens() {
86 assert_eq!(redact("token pk_123_ABC end"), "token pk_*** end");
87 assert_eq!(redact("pk_a,pk_b"), "pk_***,pk_***");
88 assert_eq!(redact("nothing here"), "nothing here");
89 }
90
91 #[test]
92 fn resolve_message_has_suggestions() {
93 let e = ClickUpError::resolve("list", "Sprnt", vec!["Sprint".into()]);
94 assert_eq!(
95 e.to_string(),
96 "Could not resolve list 'Sprnt' (did you mean: Sprint?)"
97 );
98 }
99}