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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! The one public error type.
//!
//! The pipelines keep the crate-private internal error (`AsxError` with an
//! `ErrorCode`); [`Error::from_internal`] is the single function that maps it
//! to an [`ErrorKind`] at the API boundary.
use crate::audit::AuditRecord;
use crate::core::{AsxError, ErrorCode};
use std::fmt;
/// A boxed, `Send` future โ the return type of every trait method an embedder
/// implements.
pub type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
/// The error type an embedder's trait implementation returns.
pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;
/// `Result` with [`Error`] as the default error.
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// The one error type. Opaque; ask it questions.
///
/// - [`kind`](Error::kind) says what happened, in the terms a caller acts on.
/// - [`is_retryable`](Error::is_retryable) says whether sending the *same*
/// message again (same message id and timestamp) is safe and may succeed.
/// - [`ebms_code`](Error::ebms_code) and [`mdn_disposition`](Error::mdn_disposition)
/// name the partner's or our own fault.
/// - [`evidence`](Error::evidence) returns the audit record that exists
/// despite the error.
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
message: String,
retryable: bool,
ebms: Option<String>,
mdn: Option<String>,
evidence: Option<Box<AuditRecord>>,
source: Option<BoxError>,
}
/// What happened, in the terms a caller acts on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
/// Invalid configuration or message. Nothing was sent.
Config,
/// This side refused an inbound message, receipt or MDN.
Rejected,
/// The partner refused our message (`eb:Error`, or MDN `processed/error` /
/// `failed`).
///
/// An AS4 `eb:Error` is reported as received, not as proven: error signals
/// are usually unsigned, so anything that answered on the connection could
/// have produced it (under `Profile::bdew()` the mutually authenticated,
/// SM-PKI-verified TLS channel narrows that to the partner's endpoint).
/// Treat it as a routing hint โ [`Error::is_retryable`] is true only for
/// `EBMS:0202` DeliveryFailure โ never as evidence of the message's fate;
/// only a verified receipt is. Before dead-lettering on another code, keep
/// the message and its id: resending it is safe, because receivers detect
/// duplicates.
PartnerError,
/// Definitely not delivered (connect, DNS, TLS, or request not written).
/// Resend the same message.
Transport,
/// Possibly delivered: timeout after writing, no or unparseable signal, or
/// a receipt/MDN that failed verification. Resend the same message, with
/// the same message id and timestamp (AS4 Profile 1.0 ยง3.2).
Indeterminate,
/// An embedder backend failed: `DedupStore`, `AuditSink`, `Signer`,
/// `Decrypter`.
Backend,
}
impl ErrorKind {
fn as_str(self) -> &'static str {
match self {
Self::Config => "configuration error",
Self::Rejected => "rejected",
Self::PartnerError => "partner error",
Self::Transport => "transport error",
Self::Indeterminate => "indeterminate",
Self::Backend => "backend error",
}
}
}
/// Where an internal failure happened; selects the row of the mapping table.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(any(feature = "as2", feature = "as4")), allow(dead_code))]
pub(crate) enum Stage {
/// `build()`, send-time validation, certificate and key parsing.
Config,
/// The request was not written (connect, DNS, TLS, address pin).
HttpNotWritten,
/// The request may have been written (timeout, reset, custom client).
HttpAfterWrite,
/// The partner's receipt or MDN failed verification.
ResponseVerify,
/// `verify_async_mdn`: the request is not an MDN for this `PendingMdn`.
AsyncMdnMatch,
/// `Smp::lookup`.
SmpLookup,
/// An inbound message we refused.
Inbound,
}
impl Error {
/// What happened, in the terms a caller acts on.
pub fn kind(&self) -> ErrorKind {
self.kind
}
/// Whether sending the *same* `Message` again (same message id and
/// timestamp) is safe and may succeed. True for `Transport`,
/// `Indeterminate`, `PartnerError` with EBMS:0202 or
/// `unexpected-processing-error`, and `Backend` unless the evidence shows
/// delivery.
pub fn is_retryable(&self) -> bool {
self.retryable
}
/// The ebMS3 error code of a `Rejected`/`PartnerError` AS4 fault, e.g.
/// `"EBMS:0202"`.
pub fn ebms_code(&self) -> Option<&str> {
self.ebms.as_deref()
}
/// The AS2 disposition of a `Rejected`/`PartnerError` AS2 fault, e.g.
/// `"processed/error: decryption-failed"`.
pub fn mdn_disposition(&self) -> Option<&str> {
self.mdn.as_deref()
}
/// Evidence that exists despite the error: the unverifiable receipt bytes
/// (`Indeterminate`), the partner's `eb:Error`/negative MDN
/// (`PartnerError`), or the verified receipt when only the audit record
/// failed (`Backend`, delivered).
pub fn evidence(&self) -> Option<&AuditRecord> {
self.evidence.as_deref()
}
// โโ crate-private construction โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
pub(crate) fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
let retryable = matches!(
kind,
ErrorKind::Transport | ErrorKind::Indeterminate | ErrorKind::Backend
);
Self {
kind,
message: message.into(),
retryable,
ebms: None,
mdn: None,
evidence: None,
source: None,
}
}
pub(crate) fn config(message: impl Into<String>) -> Self {
Self::new(ErrorKind::Config, message)
}
/// A failure of an embedder's backend (`Signer`, `Decrypter`,
/// `DedupStore`, `AuditSink`).
pub(crate) fn backend(message: impl Into<String>, source: BoxError) -> Self {
Self::new(ErrorKind::Backend, message).with_source(source)
}
/// The partner refused our message with an ebMS3 error.
pub(crate) fn partner_ebms(code: impl Into<String>, message: impl Into<String>) -> Self {
let code = code.into();
let mut err = Self::new(ErrorKind::PartnerError, message);
err.retryable = code == "EBMS:0202";
err.ebms = Some(code);
err
}
/// The partner refused our message with a negative MDN.
#[cfg_attr(not(feature = "as2"), allow(dead_code))]
pub(crate) fn partner_mdn(disposition: impl Into<String>, message: impl Into<String>) -> Self {
let disposition = disposition.into();
let mut err = Self::new(ErrorKind::PartnerError, message);
err.retryable = disposition.contains("unexpected-processing-error");
err.mdn = Some(disposition);
err
}
/// A partner error answered with an HTTP 5xx status is the receiver's
/// transient failure (its store or sink), so resending may succeed.
#[cfg_attr(not(any(feature = "as2", feature = "as4")), allow(dead_code))]
pub(crate) fn retryable_if(mut self, condition: bool) -> Self {
self.retryable |= condition;
self
}
pub(crate) fn with_source(mut self, source: BoxError) -> Self {
self.source = Some(source);
self
}
pub(crate) fn with_evidence(mut self, record: Option<AuditRecord>) -> Self {
self.evidence = record.map(Box::new);
self
}
/// The audit record could not be written after a verified receipt: the
/// message was delivered, so resending is not what the caller should do.
pub(crate) fn delivered_but_unrecorded(source: Self, record: AuditRecord) -> Self {
let mut err = Self::new(
ErrorKind::Backend,
format!("the message was delivered but its audit record was not written: {source}"),
);
err.retryable = false;
err.evidence = Some(Box::new(record));
err.source = Some(Box::new(source));
err
}
/// An internal error with no better stage information: classified by its
/// code alone (a configuration problem only where the code says so).
pub(crate) fn internal(err: AsxError) -> Self {
Self::from_internal(Stage::Config, err)
}
/// The one mapping from the internal error to a public kind.
pub(crate) fn from_internal(stage: Stage, err: AsxError) -> Self {
let kind = match (stage, err.code) {
(_, ErrorCode::CapacityExhausted) => ErrorKind::Transport,
(_, ErrorCode::StorageBackendFailure | ErrorCode::ReliabilityFailure) => {
ErrorKind::Backend
}
(Stage::Config, _) => ErrorKind::Config,
(Stage::HttpNotWritten, ErrorCode::TransportFailure) => ErrorKind::Transport,
// The address pin and the HTTPS rule refuse before
// anything is written: not delivered, and not worth a retry.
(
Stage::HttpNotWritten,
ErrorCode::InvalidInput | ErrorCode::PolicyViolation | ErrorCode::NotFound,
) => ErrorKind::Transport,
(Stage::HttpNotWritten | Stage::HttpAfterWrite, _) => ErrorKind::Indeterminate,
(Stage::ResponseVerify, _) => ErrorKind::Indeterminate,
(Stage::AsyncMdnMatch, _) => ErrorKind::Rejected,
(Stage::SmpLookup, ErrorCode::TransportFailure | ErrorCode::Timeout) => {
ErrorKind::Transport
}
(Stage::SmpLookup, ErrorCode::NotFound | ErrorCode::InvalidInput) => ErrorKind::Config,
(Stage::SmpLookup, _) => ErrorKind::Rejected,
(Stage::Inbound, _) => ErrorKind::Rejected,
};
let mut out = Self::new(kind, err.message.clone());
let address_pin_refusal = stage == Stage::HttpNotWritten
&& matches!(
err.code,
ErrorCode::InvalidInput | ErrorCode::PolicyViolation | ErrorCode::NotFound
);
out.retryable = match kind {
ErrorKind::Transport => !address_pin_refusal,
ErrorKind::Indeterminate | ErrorKind::Backend => true,
_ => false,
};
out.source = Some(Box::new(err));
out
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.kind.as_str(), self.message)?;
if let Some(code) = &self.ebms {
write!(f, " ({code})")?;
}
if let Some(disposition) = &self.mdn {
write!(f, " ({disposition})")?;
}
Ok(())
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_deref()
.map(|s| s as &(dyn std::error::Error + 'static))
}
}
/// Test hooks for `tests/error_mapping.rs`: drive the mapping table without a
/// pipeline. Not part of the public API.
#[doc(hidden)]
pub mod __mapping {
use super::*;
use crate::core::ErrorContext;
/// The stages the table distinguishes, by name.
#[doc(hidden)]
pub fn map(stage: &str, code: ErrorCode) -> Option<Error> {
let stage = match stage {
"config" => Stage::Config,
"http_not_written" => Stage::HttpNotWritten,
"http_after_write" => Stage::HttpAfterWrite,
"response_verify" => Stage::ResponseVerify,
"async_mdn_match" => Stage::AsyncMdnMatch,
"smp_lookup" => Stage::SmpLookup,
"inbound" => Stage::Inbound,
_ => return None,
};
Some(Error::from_internal(
stage,
AsxError::new(code, "mapped", ErrorContext::new("error_mapping")),
))
}
/// A partner `eb:Error` with `code`.
#[doc(hidden)]
pub fn partner_ebms(code: &str) -> Error {
Error::partner_ebms(code, "partner refused")
}
/// A negative MDN with `disposition`.
#[doc(hidden)]
pub fn partner_mdn(disposition: &str) -> Error {
Error::partner_mdn(disposition, "partner refused")
}
/// A backend failure (signer, decrypter, store, sink).
#[doc(hidden)]
pub fn backend() -> Error {
Error::backend("backend failed", "down".into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::ErrorContext;
fn assert_send_sync_static<T: Send + Sync + 'static>() {}
#[test]
fn error_is_send_sync_static() {
assert_send_sync_static::<Error>();
assert_send_sync_static::<ErrorKind>();
}
#[test]
fn display_has_no_trailing_period() {
let err = Error::config("partner endpoint must be https");
let text = err.to_string();
assert!(!text.ends_with('.'), "{text}");
assert!(text.starts_with("configuration error: "), "{text}");
}
#[test]
fn source_is_preserved() {
let internal = AsxError::new(
ErrorCode::TransportFailure,
"connection refused",
ErrorContext::new("unit"),
);
let err = Error::from_internal(Stage::HttpNotWritten, internal);
assert_eq!(err.kind(), ErrorKind::Transport);
let source = std::error::Error::source(&err).expect("source kept");
assert!(source.to_string().contains("connection refused"));
}
#[test]
fn partner_retry_rule() {
assert!(Error::partner_ebms("EBMS:0202", "x").is_retryable());
assert!(!Error::partner_ebms("EBMS:0004", "x").is_retryable());
assert!(
Error::partner_mdn("processed/error: unexpected-processing-error", "x").is_retryable()
);
assert!(!Error::partner_mdn("processed/error: decryption-failed", "x").is_retryable());
}
}