loops_so/error.rs
1use std::error::Error as StdError;
2
3/// Every way a [`LoopsClient`](crate::LoopsClient) call can fail.
4#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum LoopsError {
7 /// Loops answered with a failure status, or with a body this SDK did not expect.
8 #[error("{}", with_status(*.status, message))]
9 Api {
10 /// Human-readable description, including the `message` Loops sent when it sent one.
11 message: String,
12 /// HTTP status code of the response.
13 status: Option<u16>,
14 /// The response body, when it was JSON.
15 body: Option<serde_json::Value>,
16 },
17
18 /// The arguments were rejected before any request was sent.
19 #[error("{0}")]
20 InvalidInput(String),
21
22 /// The client is missing configuration it needs, such as an API key.
23 #[error("{0}")]
24 Configuration(String),
25
26 /// `create_contact` found a contact with the same email or user ID (HTTP 409).
27 #[error("[409] {message}")]
28 ContactExists {
29 /// The message Loops sent.
30 message: String,
31 },
32
33 /// Safe mode is on and the address is outside the allowed domains.
34 #[error(
35 "Email '{email}' is not allowed in safe mode. Allowed domains: {}",
36 allowed_domains.join(", ")
37 )]
38 UnsafeEmail {
39 /// The rejected address.
40 email: String,
41 /// The domains safe mode allows.
42 allowed_domains: Vec<String>,
43 },
44
45 /// Loops rate-limited the request (HTTP 429).
46 #[error("[429] Rate limit exceeded")]
47 RateLimit {
48 /// The `x-ratelimit-limit` header, or 0 when absent.
49 limit: u64,
50 /// The `x-ratelimit-remaining` header, or 0 when absent.
51 remaining: u64,
52 },
53
54 /// The request could not be sent or its response could not be read.
55 #[error("HTTP transport error: {0}")]
56 Http(#[from] reqwest::Error),
57
58 /// A success response did not match the shape the OpenAPI spec promises.
59 #[error("[{status}] Failed to parse {action} response: {source}")]
60 Decode {
61 /// The operation that was being performed, e.g. `"get campaign"`.
62 action: &'static str,
63 /// HTTP status code of the response.
64 status: u16,
65 /// The underlying parse error.
66 source: Box<dyn StdError + Send + Sync>,
67 },
68}
69
70impl LoopsError {
71 /// The HTTP status code tied to this error, when there is one.
72 pub fn status_code(&self) -> Option<u16> {
73 match self {
74 Self::Api { status, .. } => *status,
75 Self::ContactExists { .. } => Some(409),
76 Self::RateLimit { .. } => Some(429),
77 Self::Decode { status, .. } => Some(*status),
78 Self::Http(err) => err.status().map(|s| s.as_u16()),
79 Self::InvalidInput(_) | Self::Configuration(_) | Self::UnsafeEmail { .. } => None,
80 }
81 }
82
83 /// The JSON body Loops answered a failed request with, when there is one.
84 pub fn response_body(&self) -> Option<&serde_json::Value> {
85 match self {
86 Self::Api { body, .. } => body.as_ref(),
87 _ => None,
88 }
89 }
90}
91
92fn with_status(status: Option<u16>, message: &str) -> String {
93 match status {
94 Some(status) => format!("[{status}] {message}"),
95 None => message.to_owned(),
96 }
97}
98
99/// Result alias used throughout the SDK.
100pub type Result<T, E = LoopsError> = std::result::Result<T, E>;