1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copied by wuapi-codegen from packages/sdk-codegen/templates/rust/src/error.rs. Do not edit here.
//! Errors.
use std::fmt;
use std::time::Duration;
/// Why a request failed.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// The API answered with a non-2xx status.
Api {
/// The HTTP status.
status: u16,
/// Machine-readable code, e.g. `invalid_request`, `account_not_ready`.
/// `http_<status>` when the body carries none.
code: String,
/// Human-readable explanation.
message: String,
/// Extra context from the error body.
details: Option<serde_json::Map<String, serde_json::Value>>,
/// Value of the `x-request-id` response header, when present.
request_id: Option<String>,
/// The `Retry-After` response header, when present.
retry_after: Option<Duration>,
},
/// An attempt took longer than the timeout, and no retry was left or
/// allowed.
Timeout {
/// The per-attempt timeout that ran out.
timeout: Duration,
},
/// The request could not be sent or its response could not be read.
Network(reqwest::Error),
/// The API answered 2xx with a body that does not match the expected type.
Decode {
/// The HTTP status.
status: u16,
/// Value of the `x-request-id` response header, when present.
request_id: Option<String>,
/// What did not parse.
source: serde_json::Error,
},
/// The request body could not be serialized. Nothing was sent.
Encode(String),
/// The client is misconfigured (no API key, a bad project). Nothing was sent.
Config(String),
}
impl Error {
/// The HTTP status, when the API answered.
#[must_use]
pub fn status(&self) -> Option<u16> {
match self {
Self::Api { status, .. } | Self::Decode { status, .. } => Some(*status),
_ => None,
}
}
/// A machine-readable code: the API's own for [`Error::Api`], and
/// `timeout`, `network_error`, `invalid_response`, `invalid_request` or
/// `configuration` for the rest.
#[must_use]
pub fn code(&self) -> &str {
match self {
Self::Api { code, .. } => code,
Self::Timeout { .. } => "timeout",
Self::Network(_) => "network_error",
Self::Decode { .. } => "invalid_response",
Self::Encode(_) => "invalid_request",
Self::Config(_) => "configuration",
}
}
/// The request id, when the API sent one. Quote it to support.
#[must_use]
pub fn request_id(&self) -> Option<&str> {
match self {
Self::Api { request_id, .. } | Self::Decode { request_id, .. } => request_id.as_deref(),
_ => None,
}
}
/// How long the API asked to wait before trying again.
#[must_use]
pub fn retry_after(&self) -> Option<Duration> {
match self {
Self::Api { retry_after, .. } => *retry_after,
_ => None,
}
}
/// Extra context from the error body (`field` names the offending field).
#[must_use]
pub fn details(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
match self {
Self::Api { details, .. } => details.as_ref(),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Api {
status,
code,
message,
request_id,
..
} => {
write!(f, "{message} ({status} {code}")?;
if let Some(id) = request_id {
write!(f, ", request {id}")?;
}
f.write_str(")")
}
Self::Timeout { timeout } => {
write!(f, "request timed out after {} ms", timeout.as_millis())
}
Self::Network(error) => write!(f, "network error: {error}"),
Self::Decode { status, source, .. } => {
write!(f, "the {status} response did not parse: {source}")
}
Self::Encode(message) => write!(f, "the request body did not serialize: {message}"),
Self::Config(message) => f.write_str(message),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Network(error) => Some(error),
Self::Decode { source, .. } => Some(source),
_ => None,
}
}
}
/// Why a webhook request was rejected.
#[derive(Debug)]
#[non_exhaustive]
pub enum WebhookVerificationError {
/// The signature header is empty.
MissingHeader,
/// The signing secret is empty.
MissingSecret,
/// The signature header has no timestamp or no `v1` signature.
MalformedHeader,
/// The timestamp is further from now than the tolerance.
TimestampOutOfTolerance,
/// No signature in the header matches the body.
SignatureMismatch,
/// The signature is valid but the body is not the expected JSON.
InvalidBody(serde_json::Error),
}
impl fmt::Display for WebhookVerificationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingHeader => f.write_str("missing webhook signature header"),
Self::MissingSecret => f.write_str("missing webhook secret"),
Self::MalformedHeader => f.write_str("malformed webhook signature header"),
Self::TimestampOutOfTolerance => {
f.write_str("webhook timestamp is outside the tolerance window")
}
Self::SignatureMismatch => f.write_str("webhook signature does not match"),
Self::InvalidBody(error) => write!(f, "webhook body is not valid: {error}"),
}
}
}
impl std::error::Error for WebhookVerificationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidBody(error) => Some(error),
_ => None,
}
}
}