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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! JMAP error objects (RFC 8620): the method-level error returned in a
//! failed method response (§3.6.1) and the per-object `Foo/set` error
//! (§5.3).
use core::{error::Error, fmt};
use alloc::{string::String, vec::Vec};
use serde::{Deserialize, Serialize};
/// A JMAP method-level error (RFC 8620 §3.6.1).
///
/// NOTE: variants keep the struct shape even with a single field, the
/// internally-tagged serde representation of the wire object requires
/// it.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum JmapMethodError {
/// An unexpected or unknown error occurred during the method call.
ServerFail {
/// Optional human-readable detail.
description: Option<String>,
},
/// Some, but not all, expected changes were applied.
ServerPartialFail,
/// The server is currently unable to run the method.
ServerUnavailable {
/// Optional human-readable detail.
description: Option<String>,
},
/// The method requires a capability the request did not declare.
UnknownCapability {
/// Optional human-readable detail.
description: Option<String>,
},
/// The request body was not valid JSON.
NotJson {
/// Optional human-readable detail.
description: Option<String>,
},
/// The request parsed as JSON but is not a valid Request object.
NotRequest {
/// Optional human-readable detail.
description: Option<String>,
},
/// A server-defined limit was exceeded.
Limit {
/// Optional human-readable detail.
description: Option<String>,
/// The name of the exceeded limit.
limit: String,
},
/// One of the method arguments is invalid.
InvalidArguments {
/// Optional human-readable detail.
description: Option<String>,
},
/// Access denied for this method call (RFC 8620 §3.6.2), e.g. requesting
/// the `url` or `keys` properties in `PushSubscription/get` (§7.2.1).
Forbidden {
/// Optional human-readable detail.
description: Option<String>,
},
/// The total request size exceeds the server limit.
RequestTooLarge,
/// The referenced object does not exist.
NotFound,
/// A `Foo/set` update patch is invalid.
InvalidPatch {
/// Optional human-readable detail.
description: Option<String>,
},
/// The object will be destroyed by this request, so it cannot be
/// updated.
WillDestroy {
/// Optional human-readable detail.
description: Option<String>,
},
/// One or more object properties are invalid.
InvalidProperties {
/// Optional human-readable detail.
description: Option<String>,
/// The invalid property names.
#[serde(default)]
properties: Vec<String>,
},
/// The type is a singleton, objects cannot be created or destroyed.
Singleton,
/// The method name is not known by the server.
UnknownMethod {
/// Optional human-readable detail.
description: Option<String>,
},
/// Server can no longer compute changes from `sinceState` (RFC 8620 §5.2):
/// callers MUST fall back to `Foo/get` and resume from the returned state.
CannotCalculateChanges {
/// Optional human-readable detail.
description: Option<String>,
},
/// Any error type this library does not know about.
#[serde(other)]
Unknown,
}
impl fmt::Display for JmapMethodError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ServerFail { description } => {
write!(f, "JMAP serverFail")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::ServerPartialFail => write!(f, "JMAP serverPartialFail"),
Self::ServerUnavailable { description } => {
write!(f, "JMAP serverUnavailable")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::UnknownCapability { description } => {
write!(f, "JMAP unknownCapability")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::NotJson { description } => {
write!(f, "JMAP notJson")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::NotRequest { description } => {
write!(f, "JMAP notRequest")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::Limit { description, limit } => {
write!(f, "JMAP limit ({limit})")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::InvalidArguments { description } => {
write!(f, "JMAP invalidArguments")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::Forbidden { description } => {
write!(f, "JMAP forbidden")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::RequestTooLarge => write!(f, "JMAP requestTooLarge"),
Self::NotFound => write!(f, "JMAP notFound"),
Self::InvalidPatch { description } => {
write!(f, "JMAP invalidPatch")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::WillDestroy { description } => {
write!(f, "JMAP willDestroy")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::InvalidProperties {
description,
properties,
} => {
write!(f, "JMAP invalidProperties")?;
if !properties.is_empty() {
write!(f, " [{}]", properties.join(", "))?;
}
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::Singleton => write!(f, "JMAP singleton"),
Self::UnknownMethod { description } => {
write!(f, "JMAP unknownMethod")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::CannotCalculateChanges { description } => {
write!(f, "JMAP cannotCalculateChanges")?;
if let Some(d) = description {
write!(f, ": {d}")?;
}
Ok(())
}
Self::Unknown => write!(f, "JMAP unknown error"),
}
}
}
impl Error for JmapMethodError {}
/// Per-object error returned in `Foo/set` responses (RFC 8620 §5.3).
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapSetError {
/// The wire error type (`invalidProperties`, `forbidden`, …).
pub r#type: String,
/// Optional human-readable detail.
pub description: Option<String>,
/// The properties the error relates to, when applicable.
#[serde(default)]
pub properties: Vec<String>,
}