rama-net 0.4.0

rama network types and utilities
Documentation
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use core::{convert::Infallible, fmt};

use rama_core::{
    error::{BoxError, ErrorExt as _, extra::OpaqueError},
    telemetry::tracing,
};

/// The architectural domain in which establishing a client connection failed.
///
/// Domains describe the role a protocol or component plays in a connector stack
/// rather than assigning protocols to fixed OSI layers. For example, TLS to a
/// proxy is part of [`Transport`](Self::Transport), while TLS to the origin is
/// part of [`Application`](Self::Application).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ConnectionErrorDomain {
    /// Establishing the usable transport path selected for the connection.
    ///
    /// This is the route-dependent domain. A route planner can generally use
    /// it as the signal that trying the next route may produce a different
    /// outcome.
    Transport,
    /// Establishing the end-to-end connection over the selected transport path.
    ///
    /// This is not limited to a strict OSI application-layer protocol. It also
    /// includes protocols and handshakes between the transport path and the final
    /// application protocol, such as TLS to the origin. Which domain a protocol
    /// belongs to depends on its role: TLS to a proxy is transport establishment,
    /// while TLS to the origin is end-to-end application establishment.
    /// A different transport route should not normally be tried for these
    /// failures because the selected route was already usable.
    Application,
    /// Client-local connection acquisition or orchestration failed.
    ///
    /// These failures originate in the connector stack itself rather than in a
    /// remote route or application peer. Examples include invalid local
    /// configuration or input, connection-pool bookkeeping failures, exhaustion
    /// of a local resource, and cancellation of the overall connection attempt.
    /// These failures are not specific to a selected route.
    Local,
    /// The failure has not been classified. Consumers should not assume that
    /// trying another route is safe merely because the domain is unknown.
    Unknown,
}

impl fmt::Display for ConnectionErrorDomain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Transport => "transport",
            Self::Application => "application",
            Self::Local => "local",
            Self::Unknown => "unknown",
        })
    }
}

/// A protocol-independent description of what prevented connection setup.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ConnectionErrorKind {
    /// An endpoint, route, or required resource was unavailable.
    Unavailable,
    /// Connection setup exceeded its allowed duration.
    Timeout,
    /// A peer explicitly rejected the connection operation.
    Rejected,
    /// Connection setup requires authentication or authentication failed.
    Authentication,
    /// A protocol handshake or negotiation failed.
    Protocol,
    /// The connection input or configuration is invalid.
    InvalidInput,
    /// Local connection machinery failed unexpectedly.
    Internal,
    /// The failure does not fit another kind.
    Other,
}

impl fmt::Display for ConnectionErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Unavailable => "unavailable",
            Self::Timeout => "timeout",
            Self::Rejected => "rejected",
            Self::Authentication => "authentication",
            Self::Protocol => "protocol",
            Self::InvalidInput => "invalid-input",
            Self::Internal => "internal",
            Self::Other => "other",
        })
    }
}

/// A classified error produced while establishing a client connection.
///
/// The original error remains available as the source. Adding context only
/// wraps that source and therefore preserves the classification.
#[must_use = "a connection error should be returned or handled"]
pub struct ConnectionError {
    source: BoxError,
    domain: ConnectionErrorDomain,
    kind: ConnectionErrorKind,
}

impl ConnectionError {
    /// Create a classified connection error.
    pub fn new(
        source: impl Into<BoxError>,
        domain: ConnectionErrorDomain,
        kind: ConnectionErrorKind,
    ) -> Self {
        Self {
            source: source.into(),
            domain,
            kind,
        }
    }

    /// Create an error which occurred while establishing the transport path.
    pub fn transport(source: impl Into<BoxError>, kind: ConnectionErrorKind) -> Self {
        Self::new(source, ConnectionErrorDomain::Transport, kind)
    }

    /// Create an error which occurred while establishing the end-to-end connection.
    ///
    /// This includes intermediate end-to-end protocols such as TLS to the origin,
    /// not only the final application protocol.
    pub fn application(source: impl Into<BoxError>, kind: ConnectionErrorKind) -> Self {
        Self::new(source, ConnectionErrorDomain::Application, kind)
    }

    /// Create an error produced by client-local acquisition or orchestration machinery.
    pub fn local(source: impl Into<BoxError>, kind: ConnectionErrorKind) -> Self {
        Self::new(source, ConnectionErrorDomain::Local, kind)
    }

    /// Create an unclassified connection error.
    pub fn unknown(source: impl Into<BoxError>) -> Self {
        Self::new(
            source,
            ConnectionErrorDomain::Unknown,
            ConnectionErrorKind::Other,
        )
    }

    /// Return the architectural domain in which the failure occurred.
    #[inline]
    pub fn domain(&self) -> ConnectionErrorDomain {
        self.domain
    }

    /// Return the protocol-independent kind of failure.
    #[inline]
    pub fn kind(&self) -> ConnectionErrorKind {
        self.kind
    }

    /// Return the original error, including any attached context.
    #[inline]
    pub fn get_ref(&self) -> &(dyn core::error::Error + Send + Sync + 'static) {
        self.source.as_ref()
    }

    /// Consume this error and return its boxed source.
    #[inline]
    pub fn into_source(self) -> BoxError {
        self.source
    }

    /// Convert this error into a [`BoxError`] without losing its classification.
    #[inline]
    pub fn into_box_error(self) -> BoxError {
        Box::new(self)
    }

    /// Convert this error into an [`OpaqueError`] without losing its classification.
    #[inline]
    pub fn into_opaque_error(self) -> OpaqueError {
        self.into_box_error().into_opaque_error()
    }

    /// Add context to the source error.
    pub fn context<M>(mut self, value: M) -> Self
    where
        M: fmt::Debug + fmt::Display + Send + Sync + 'static,
    {
        self.source = self.source.context(value);
        self
    }

    /// Add context using [`fmt::LowerHex`] for its formatting.
    pub fn context_hex<M>(mut self, value: M) -> Self
    where
        M: fmt::Debug + Send + Sync + 'static,
    {
        self.source = self.source.context_hex(value);
        self
    }

    /// Add context using [`fmt::Debug`] for its display formatting.
    pub fn context_debug<M>(mut self, value: M) -> Self
    where
        M: fmt::Debug + Send + Sync + 'static,
    {
        self.source = self.source.context_debug(value);
        self
    }

    /// Add keyed context to the source error.
    pub fn context_field<M>(mut self, key: &'static str, value: M) -> Self
    where
        M: fmt::Debug + fmt::Display + Send + Sync + 'static,
    {
        self.source = self.source.context_field(key, value);
        self
    }

    /// Add a keyed string-like context value to the source error.
    pub fn context_str_field<M>(mut self, key: &'static str, value: M) -> Self
    where
        M: Into<String>,
    {
        self.source = self.source.context_str_field(key, value);
        self
    }

    /// Add keyed context using [`fmt::LowerHex`] for its formatting.
    pub fn context_hex_field<M>(mut self, key: &'static str, value: M) -> Self
    where
        M: fmt::Debug + Send + Sync + 'static,
    {
        self.source = self.source.context_hex_field(key, value);
        self
    }

    /// Add keyed context using [`fmt::Debug`] for its display formatting.
    pub fn context_debug_field<M>(mut self, key: &'static str, value: M) -> Self
    where
        M: fmt::Debug + Send + Sync + 'static,
    {
        self.source = self.source.context_debug_field(key, value);
        self
    }

    /// Lazily add context to the source error.
    pub fn with_context<C, F>(mut self, create: F) -> Self
    where
        C: fmt::Debug + fmt::Display + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.source = self.source.with_context(create);
        self
    }

    /// Lazily add context using [`fmt::LowerHex`] for its formatting.
    pub fn with_context_hex<C, F>(mut self, create: F) -> Self
    where
        C: fmt::Debug + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.source = self.source.with_context_hex(create);
        self
    }

    /// Lazily add context using [`fmt::Debug`] for its display formatting.
    pub fn with_context_debug<C, F>(mut self, create: F) -> Self
    where
        C: fmt::Debug + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.source = self.source.with_context_debug(create);
        self
    }

    /// Lazily add keyed context to the source error.
    pub fn with_context_field<C, F>(mut self, key: &'static str, create: F) -> Self
    where
        C: fmt::Debug + fmt::Display + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.source = self.source.with_context_field(key, create);
        self
    }

    /// Lazily add a keyed string-like context value to the source error.
    pub fn with_context_str_field<C, F>(mut self, key: &'static str, create: F) -> Self
    where
        C: Into<String>,
        F: FnOnce() -> C,
    {
        self.source = self.source.with_context_str_field(key, create);
        self
    }

    /// Lazily add keyed context using [`fmt::LowerHex`] for its formatting.
    pub fn with_context_hex_field<C, F>(mut self, key: &'static str, create: F) -> Self
    where
        C: fmt::Debug + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.source = self.source.with_context_hex_field(key, create);
        self
    }

    /// Lazily add keyed context using [`fmt::Debug`] for its display formatting.
    pub fn with_context_debug_field<C, F>(mut self, key: &'static str, create: F) -> Self
    where
        C: fmt::Debug + Send + Sync + 'static,
        F: FnOnce() -> C,
    {
        self.source = self.source.with_context_debug_field(key, create);
        self
    }

    /// Capture a backtrace and attach it to the source error.
    pub fn backtrace(mut self) -> Self {
        self.source = self.source.backtrace();
        self
    }

    fn classification_in_source_chain(
        source: &(dyn core::error::Error + 'static),
    ) -> Option<(ConnectionErrorDomain, ConnectionErrorKind)> {
        let mut current = Some(source);
        let mut timeout = false;
        // Protect conversion from a malformed error implementation with a cyclic
        // source chain. Rama's own wrappers are shallow and acyclic.
        for _ in 0..64 {
            let Some(error) = current else {
                break;
            };
            if let Some(error) = error.downcast_ref::<Self>() {
                return Some((error.domain, error.kind));
            }
            timeout |= error.is::<rama_core::layer::timeout::Elapsed>()
                || error.is::<tokio::time::error::Elapsed>();
            current = error.source();
        }
        timeout.then_some((
            ConnectionErrorDomain::Transport,
            ConnectionErrorKind::Timeout,
        ))
    }
}

impl From<BoxError> for ConnectionError {
    fn from(source: BoxError) -> Self {
        let source = match source.downcast::<Self>() {
            Ok(error) => return *error,
            Err(source) => source,
        };

        let (domain, kind) =
            Self::classification_in_source_chain(source.as_ref()).unwrap_or_else(|| {
                tracing::debug!(
                    "connector error is unclassified; retry-based routing will treat it as terminal"
                );
                (ConnectionErrorDomain::Unknown, ConnectionErrorKind::Other)
            });

        Self {
            source,
            domain,
            kind,
        }
    }
}

impl From<Infallible> for ConnectionError {
    fn from(error: Infallible) -> Self {
        match error {}
    }
}

impl fmt::Debug for ConnectionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ConnectionError")
            .field("domain", &self.domain)
            .field("kind", &self.kind)
            .field("source", &self.source)
            .finish()
    }
}

impl fmt::Display for ConnectionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.source, f)
    }
}

impl core::error::Error for ConnectionError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        Some(self.source.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rama_core::{Layer, Service, error::ErrorContext as _};

    #[derive(Debug)]
    struct TestError(&'static str);

    impl fmt::Display for TestError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str(self.0)
        }
    }

    impl core::error::Error for TestError {}

    #[derive(Debug)]
    struct CyclicError;

    impl fmt::Display for CyclicError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("cyclic error")
        }
    }

    impl core::error::Error for CyclicError {
        fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
            Some(self)
        }
    }

    fn unavailable_transport_error() -> ConnectionError {
        ConnectionError::transport(
            TestError("connect failed"),
            ConnectionErrorKind::Unavailable,
        )
    }

    fn assert_unavailable_transport(error: &ConnectionError) {
        assert_eq!(error.domain(), ConnectionErrorDomain::Transport);
        assert_eq!(error.kind(), ConnectionErrorKind::Unavailable);
    }

    #[test]
    fn display_preserves_source_message() {
        let error = unavailable_transport_error();
        assert_eq!(error.to_string(), "connect failed");
        assert!(error.get_ref().is::<TestError>());
    }

    #[test]
    fn direct_box_roundtrip_preserves_classification() {
        let error = ConnectionError::from(unavailable_transport_error().into_box_error());
        assert_unavailable_transport(&error);
        assert!(error.get_ref().is::<TestError>());
    }

    #[test]
    fn contextual_box_roundtrip_preserves_classification() {
        let boxed = unavailable_transport_error()
            .into_box_error()
            .context("dial selected endpoint")
            .context_field("attempt", 2);

        let error = ConnectionError::from(boxed);
        assert_unavailable_transport(&error);
        let message = error.to_string();
        assert!(message.contains("dial selected endpoint"), "{message}");
        assert!(message.contains("attempt=\"2\""), "{message}");
    }

    #[tokio::test(start_paused = true)]
    async fn timeout_layer_error_is_classified_as_transport_timeout() {
        let source =
            rama_core::layer::timeout::TimeoutLayer::new(core::time::Duration::from_secs(1));
        let error = source
            .into_layer(rama_core::service::service_fn(async |(): ()| {
                core::future::pending::<Result<(), Infallible>>().await
            }))
            .serve(())
            .await
            .unwrap_err();

        let error = ConnectionError::from(error.context("connector attempt"));
        assert_eq!(error.domain(), ConnectionErrorDomain::Transport);
        assert_eq!(error.kind(), ConnectionErrorKind::Timeout);
    }

    #[test]
    fn result_context_roundtrip_preserves_classification() {
        fn contextualized() -> Result<(), ConnectionError> {
            let result: Result<(), ConnectionError> = Err(unavailable_transport_error());
            result.context("establish connection")?;
            Ok(())
        }

        let error = contextualized().unwrap_err();
        assert_unavailable_transport(&error);
        assert!(error.to_string().contains("establish connection"));
    }

    #[test]
    fn inherent_context_api_preserves_classification() {
        let error = unavailable_transport_error()
            .context("context")
            .context_hex(255)
            .context_debug(Some("debug"))
            .context_field("field", 1)
            .context_str_field("str", "value")
            .context_hex_field("hex", 255)
            .context_debug_field("debug", Some(2))
            .with_context(|| "lazy-context")
            .with_context_hex(|| 16)
            .with_context_debug(|| Some("lazy-debug"))
            .with_context_field("lazy-field", || 3)
            .with_context_str_field("lazy-str", || "lazy-value")
            .with_context_hex_field("lazy-hex", || 32)
            .with_context_debug_field("lazy-debug-field", || Some(4));

        assert_unavailable_transport(&error);
        let message = error.to_string();
        for expected in [
            "context",
            "field=\"1\"",
            "str=\"value\"",
            "lazy-context",
            "lazy-field=\"3\"",
            "lazy-str=\"lazy-value\"",
        ] {
            assert!(
                message.contains(expected),
                "missing {expected:?} in {message}"
            );
        }
    }

    #[test]
    fn unknown_box_error_gets_safe_classification() {
        let source: BoxError = Box::new(TestError("legacy error"));
        let error = ConnectionError::from(source);

        assert_eq!(error.domain(), ConnectionErrorDomain::Unknown);
        assert_eq!(error.kind(), ConnectionErrorKind::Other);
        assert_eq!(error.to_string(), "legacy error");
    }

    #[test]
    fn cyclic_source_chain_gets_safe_classification() {
        let source: BoxError = Box::new(CyclicError);
        let error = ConnectionError::from(source);

        assert_eq!(error.domain(), ConnectionErrorDomain::Unknown);
        assert_eq!(error.kind(), ConnectionErrorKind::Other);
        assert_eq!(error.to_string(), "cyclic error");
    }

    #[test]
    fn backtrace_and_opaque_roundtrip_preserve_classification() {
        let opaque = unavailable_transport_error()
            .backtrace()
            .into_opaque_error();
        let error = ConnectionError::from(opaque.into_box_error());

        assert_unavailable_transport(&error);
        assert_eq!(error.to_string(), "connect failed");
    }
}