gix-error 0.4.0

A crate of the gitoxide project to provide common errors and error-handling 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use crate::Metadata;

// Keep inherent methods on Error and Exn while sharing their implementation and documentation.
macro_rules! classification_predicates {
    () => {
        /// Return `true` if any stored error or native source has an explicit [`crate::Class::Retryable`] classification.
        ///
        /// [`crate::Message`] and [`crate::ClassificationMarker`] can supply this classification.
        /// Nested [`crate::Error`] values are inspected recursively. Unlike [`Self::can_retry()`], this does not infer
        /// retryability from I/O error kinds.
        pub fn is_retryable(&self) -> bool {
            self.classify().is_retryable()
        }

        /// Return `true` if any stored error or native source reports resource exhaustion.
        ///
        /// This recognizes messages or markers with
        /// [`crate::Class::ResourceExhaustion`], [`std::collections::TryReserveError`], and
        /// [`std::io::ErrorKind::OutOfMemory`], including within nested
        /// [`crate::Error`] values.
        pub fn is_resource_exhausted(&self) -> bool {
            self.classify().is_resource_exhausted()
        }

        /// Return `true` if any stored error, or an error in its [`source()`](std::error::Error::source) chain, is:
        ///
        /// * classified as [`crate::Class::Retryable`], or
        /// * classified as [`crate::Class::Io`] with kind `Interrupted` or `TimedOut`.
        ///
        /// Nested [`crate::Error`] values are inspected recursively. `false` only means that no known retryable error was
        /// found; it does not guarantee that retrying cannot succeed.
        pub fn can_retry(&self) -> bool {
            self.classify().can_retry()
        }

        /// Apply [`Self::can_retry()`], also accepting [`std::io::Error`] with kind `UnexpectedEof`, `OutOfMemory`,
        /// `BrokenPipe`, `AddrInUse`, `ConnectionAborted`, `ConnectionReset`, or `ConnectionRefused`.
        ///
        /// This applies a more lenient policy than [`Self::can_retry`]. Nested [`crate::Error`] values are inspected recursively.
        /// `false` only means that no known retryable error was found; it does not guarantee that retrying cannot succeed.
        pub fn can_retry_lenient(&self) -> bool {
            self.classify().can_retry_lenient()
        }

        /// Return `true` if malformed or internally inconsistent data caused the failure.
        pub fn is_corrupted(&self) -> bool {
            self.classify().is_corrupted()
        }

        /// Return `true` if a requested resource was not found.
        pub fn is_not_found(&self) -> bool {
            self.classify().is_not_found()
        }

        /// Return `true` if invalid input caused the failure.
        pub fn is_validation(&self) -> bool {
            self.classify().is_validation()
        }
    };
}

/// A borrowed error together with its optional caller location, intended for diagnostic display.
///
/// Errors owned by a [`crate::exn::Frame`] have the location captured when that frame was created. The first real source
/// beneath transparent classification markers inherits their frame's location. Other native
/// [`std::error::Error::source()`] values have no location because no caller location was captured for them.
///
/// Unlike [`crate::exn::Frame`], this type neither owns the error nor represents relationships in an error tree. This lets
/// [`crate::Error::iter_errors_with_locations()`] provide the same lightweight view for the tree-backed and flattened-chain
/// representations.
///
/// Its normal [`Display`](std::fmt::Display) output appends the location when one is available. Alternate formatting
/// (`{source:#}`) forwards alternate formatting to the underlying error and always omits the location.
#[derive(Clone, Copy, Debug)]
pub struct DisplaySource<'a> {
    error: &'a (dyn std::error::Error + 'static),
    location: Option<&'static std::panic::Location<'static>>,
}

impl<'a> DisplaySource<'a> {
    /// Return the stored error, preserving its concrete type for downcasting.
    pub fn error(&self) -> &'a (dyn std::error::Error + 'static) {
        self.error
    }

    /// Return the captured or inherited caller location, or `None` for an ordinary native error source.
    pub fn location(&self) -> Option<&'static std::panic::Location<'static>> {
        self.location
    }
}

impl std::fmt::Display for DisplaySource<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self.error, f)?;
        if !f.alternate()
            && let Some(location) = self.location
        {
            crate::write_location(f, location)?;
        }
        Ok(())
    }
}

impl crate::Error {
    /// Lazily visit stored errors and native sources in logical breadth-first order, expanding nested [`crate::Error`] values.
    ///
    /// The stored error is first unless it is a classification marker. A frame's native source precedes its explicitly
    /// raised children. Concrete error types remain available for downcasting, except for classification markers,
    /// which are always transparent to traversal.
    /// Use [`Self::classify()`] to inspect classifications.
    pub fn iter_errors(&self) -> impl Iterator<Item = &(dyn std::error::Error + 'static)> + '_ {
        self.iter_errors_with_locations().map(|source| source.error)
    }

    /// Visit the same errors as [`Self::iter_errors()`], with caller locations for explicitly raised frames.
    /// The first real source beneath transparent classification markers inherits their frame's location; other native
    /// sources have no caller location of their own. [`DisplaySource`] can render either representation.
    pub fn iter_errors_with_locations(&self) -> impl Iterator<Item = DisplaySource<'_>> + '_ {
        Errors::new(self.iter_root()).filter(|source| !is_transparent_marker(source.error))
    }

    /// Find the first diagnostic error that downcasts to `T` in logical breadth-first order.
    /// Classification markers are omitted, as in [`Self::iter_errors()`].
    pub fn downcast_any_ref<T: std::error::Error + 'static>(&self) -> Option<&T> {
        self.iter_errors().find_map(|error| error.downcast_ref())
    }

    /// Follow the unique causal path to a leaf or aggregate, as in [`crate::exn::Frame::probable_cause()`].
    ///
    /// Classification markers are always transparent to selection. Nested error graphs and explicitly raised children
    /// both participate, so a selected boundary at a branch is not replaced by one of its nested causes.
    /// If selection stays at the root, return the stored error, including a classification-only root.
    pub fn probable_cause(&self) -> &(dyn std::error::Error + 'static) {
        self.iter_root().probable_cause().unwrap_or_else(|| self.error())
    }

    /// Visit the non-empty [`Metadata`] dictionaries of [`crate::Message`] contexts in error traversal order.
    /// Dictionaries remain separate. Functions returning metadata document the keys in each context.
    ///
    /// To match a class and values on the same message, use [`Self::classify()`] and
    /// [`Classification::error()`](crate::types::Classification::error) instead of combining independent classification
    /// and metadata searches.
    pub fn metadata(&self) -> impl Iterator<Item = &Metadata> + '_ {
        self.iter_errors()
            .filter_map(|error| error.downcast_ref::<crate::Message>())
            .map(|error| &error.values)
            .filter(|values| !values.is_empty())
    }

    /// Return all known classifications in the same logical breadth-first order as [`Self::iter_errors()`].
    ///
    /// Unknown errors are omitted. Classifications aren't deduplicated because distinct errors may independently have
    /// the same meaning. Each item retains the classified error for downcasting and origin inspection.
    pub fn classify(&self) -> Classifications<'_> {
        classify(self)
    }

    classification_predicates!();
}

/// Classification helpers for inspecting an exception without consuming it or losing its typed outer error.
///
/// The corresponding helpers on [`crate::Error`] would require consuming the exception with
/// [`into_error()`](crate::Exn::into_error), while dereferencing an exception only exposes its outer error `E`, not
/// the full error tree. These helpers inspect that tree directly, so callers can recognize a failure's meaning
/// even when it is wrapped in context, and still propagate the original exception afterward.
impl<E: std::error::Error + Send + Sync + 'static> crate::Exn<E> {
    /// Return all known classifications in logical breadth-first order, including native sources and nested
    /// [`crate::Error`] values.
    ///
    /// As with [`crate::Error::classify()`], unknown errors are omitted, classifications aren't deduplicated, and each
    /// item retains the classified error for downcasting and origin inspection.
    pub fn classify(&self) -> Classifications<'_> {
        Classifications(Errors::new(Node::Frame(self.frame())))
    }

    classification_predicates!();
}

/// The semantic class of an error.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Class {
    /// Function or method input was invalid.
    Validation,
    /// Stored or streamed data was malformed or internally inconsistent.
    Corruption,
    /// A requested resource does not exist.
    NotFound,
    /// Retrying the operation may succeed.
    Retryable,
    /// A finite resource was exhausted.
    ResourceExhaustion(crate::ResourceExhaustionKind),
    /// An I/O failure not normalized to another semantic class.
    Io(std::io::ErrorKind),
    /// An operation-specific condition identified by a stable, namespaced string.
    ///
    /// Functions returning this class document their tags. Tags imply no other classification;
    /// match them with [`Classifications::has()`] instead of inspecting diagnostic text or metadata.
    Tagged(&'static str),
}

/// A semantic class together with the concrete error which established it.
#[derive(Clone, Copy, Debug)]
pub struct Classification<'a> {
    class: Class,
    error: &'a (dyn std::error::Error + 'static),
}

/// Lazily inspect the classifications of any borrowed error, including its native sources, I/O payloads and nested
/// [`crate::Error`] values. Unknown errors are omitted and distinct causes may yield the same classification.
///
/// ```
/// let error = std::io::Error::other(gix_error::not_found("missing object"));
/// assert!(gix_error::classify(&error).is_not_found());
/// ```
pub fn classify<'a>(err: &'a (dyn std::error::Error + 'static)) -> Classifications<'a> {
    Classifications(Errors::new(err.downcast_ref::<crate::Error>().map_or(
        Node::Source {
            error: err,
            location: None,
        },
        crate::Error::iter_root,
    )))
}

/// A lazy iterator over classified causes. Its predicates consume the remaining iterator and stop at the first match.
pub struct Classifications<'a>(Errors<'a>);

impl<'a> Iterator for Classifications<'a> {
    type Item = Classification<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.find_map(|source| classify_one(source.error))
    }
}

impl Classifications<'_> {
    /// Return whether any remaining cause is explicitly marked as retryable.
    pub fn is_retryable(self) -> bool {
        self.has(Class::Retryable)
    }

    /// Apply the conservative retry policy of [`crate::Error::can_retry()`] to the remaining causes.
    pub fn can_retry(mut self) -> bool {
        self.any(|classification| class_can_retry(classification.class()))
    }

    /// Apply the broader I/O policy of [`crate::Error::can_retry_lenient()`] to the remaining causes.
    pub fn can_retry_lenient(mut self) -> bool {
        self.any(classification_can_retry_lenient)
    }

    /// Return whether any remaining cause reports a missing resource.
    pub fn is_not_found(self) -> bool {
        self.has(Class::NotFound)
    }

    /// Return whether any remaining cause reports invalid input.
    pub fn is_validation(self) -> bool {
        self.has(Class::Validation)
    }

    /// Return whether any remaining cause reports malformed or inconsistent data.
    pub fn is_corrupted(self) -> bool {
        self.has(Class::Corruption)
    }

    /// Return whether any remaining cause reports resource exhaustion.
    pub fn is_resource_exhausted(mut self) -> bool {
        self.any(|classification| matches!(classification.class(), Class::ResourceExhaustion(_)))
    }

    /// Return whether any remaining cause has exactly `class`, including its tag for [`Class::Tagged`].
    pub fn has(mut self, class: Class) -> bool {
        self.any(|classification| classification.class() == class)
    }
}

impl<'a> Classification<'a> {
    /// Return the semantic class.
    pub fn class(&self) -> Class {
        self.class
    }

    /// Return the concrete error which established the classification.
    pub fn error(&self) -> &'a (dyn std::error::Error + 'static) {
        self.error
    }

    /// Return the original I/O error kind, if the underlying error is an [`std::io::Error`].
    pub fn io_kind(&self) -> Option<std::io::ErrorKind> {
        self.error.downcast_ref::<std::io::Error>().map(std::io::Error::kind)
    }
}

fn classify_one<'a>(error: &'a (dyn std::error::Error + 'static)) -> Option<Classification<'a>> {
    let class = if let Some(marker) = error.downcast_ref::<crate::ClassificationMarker>() {
        marker.class()
    } else if let Some(error) = error.downcast_ref::<crate::Message>() {
        error.class?
    } else if error.is::<std::collections::TryReserveError>() {
        Class::ResourceExhaustion(crate::ResourceExhaustionKind::AllocationFailure)
    } else {
        let error = error.downcast_ref::<std::io::Error>()?;
        match error.kind() {
            std::io::ErrorKind::NotFound => Class::NotFound,
            std::io::ErrorKind::OutOfMemory => {
                Class::ResourceExhaustion(crate::ResourceExhaustionKind::AllocationFailure)
            }
            kind => Class::Io(kind),
        }
    };
    Some(Classification { class, error })
}

fn class_can_retry(class: Class) -> bool {
    matches!(
        class,
        Class::Retryable | Class::Io(std::io::ErrorKind::Interrupted | std::io::ErrorKind::TimedOut)
    )
}

fn classification_can_retry_lenient(classification: Classification<'_>) -> bool {
    class_can_retry(classification.class())
        || classification.io_kind().is_some_and(|kind| {
            use std::io::ErrorKind::*;
            matches!(
                kind,
                UnexpectedEof
                    | OutOfMemory
                    | BrokenPipe
                    | AddrInUse
                    | ConnectionAborted
                    | ConnectionReset
                    | ConnectionRefused
            )
        })
}

#[derive(Clone, Copy)]
enum Node<'a> {
    Frame(&'a crate::exn::Frame),
    Source {
        error: &'a (dyn std::error::Error + 'static),
        location: Option<&'static std::panic::Location<'static>>,
    },
    #[cfg(all(feature = "auto-chain-error", not(feature = "tree-error")))]
    Chain {
        node: &'a crate::types::ChainedError,
        index: usize,
        cursor: Option<usize>,
    },
}

impl<'a> Node<'a> {
    fn display(self) -> DisplaySource<'a> {
        let (error, location) = match self {
            Node::Frame(frame) => (
                frame.error() as &(dyn std::error::Error + 'static),
                Some(frame.location()),
            ),
            Node::Source { error, location } => (error, location),
            #[cfg(all(feature = "auto-chain-error", not(feature = "tree-error")))]
            Node::Chain { node, .. } => (node.err.error(), node.err.has_frame_location().then_some(node.location)),
        };
        DisplaySource { error, location }
    }

    fn children(self) -> std::collections::VecDeque<Node<'a>> {
        // Cause selection follows a path rather than breadth-first order, so each query needs its own chain cursor.
        #[cfg(all(feature = "auto-chain-error", not(feature = "tree-error")))]
        let root = match self {
            Node::Chain { node, index, .. } => Node::Chain {
                node,
                index,
                cursor: None,
            },
            root => root,
        };
        #[cfg(any(feature = "tree-error", not(feature = "auto-chain-error")))]
        let root = self;
        let mut traversal = Errors::new(root);
        traversal.children(root);
        traversal.pending
    }

    fn probable_cause(self) -> Option<&'a (dyn std::error::Error + 'static)> {
        let mut node = self;
        // Track traversal, not error addresses: a native source can share its owner's address.
        let mut cause = None;
        loop {
            let mut pending = node.children();
            let mut only_child = None;
            while let Some(child) = pending.pop_front() {
                if is_transparent_marker(child.display().error) {
                    // Marker frames (including nested boundaries storing markers) are transparent, not dead ends.
                    pending.extend(child.children());
                } else if only_child.replace(child).is_some() {
                    return cause;
                }
            }
            node = match only_child {
                Some(child) => child,
                None => return cause,
            };
            cause = Some(node.display().error);
        }
    }
}

struct Errors<'a> {
    root: Option<Node<'a>>,
    previous: Option<Node<'a>>,
    pending: std::collections::VecDeque<Node<'a>>,
    #[cfg(all(feature = "auto-chain-error", not(feature = "tree-error")))]
    chains: Vec<(usize, Option<&'a crate::types::ChainedError>)>,
}

impl<'a> Errors<'a> {
    fn new(root: Node<'a>) -> Self {
        Errors {
            root: Some(root),
            previous: None,
            pending: Default::default(),
            #[cfg(all(feature = "auto-chain-error", not(feature = "tree-error")))]
            chains: Vec::new(),
        }
    }

    fn source(
        &mut self,
        error: &'a (dyn std::error::Error + 'static),
        location: Option<&'static std::panic::Location<'static>>,
    ) {
        if let Some(error) = error.downcast_ref::<crate::Error>() {
            self.pending.push_back(error.iter_root());
        } else if let Some(source) = native_source(error) {
            self.pending.push_back(Node::Source {
                error: source,
                location: location.filter(|_| is_transparent_marker(error)),
            });
        }
    }

    fn children(&mut self, node: Node<'a>) {
        match node {
            Node::Frame(frame) => {
                self.source(frame.error(), Some(frame.location()));
                self.pending.extend(frame.children().iter().map(Node::Frame));
            }
            Node::Source { error, location } => self.source(error, location),
            #[cfg(all(feature = "auto-chain-error", not(feature = "tree-error")))]
            Node::Chain { node, index, cursor } => {
                if let Some(error) = node.err.error().downcast_ref::<crate::Error>() {
                    self.pending.push_back(error.iter_root());
                }
                let cursor = match cursor {
                    Some(cursor) => cursor,
                    None if node.source.is_none() => return,
                    None => {
                        self.chains.push((index + 1, node.source.as_deref()));
                        self.chains.len() - 1
                    }
                };
                // Flattened parents occur in increasing order. One cursor per boundary streams each child once,
                // even when other error trees are interleaved at their logical breadth-first positions.
                let (child_index, next) = &mut self.chains[cursor];
                // A fresh cursor for cause selection can start among siblings belonging to earlier parents.
                while let Some(child) = next.filter(|child| child.logical_parent.is_some_and(|parent| parent < index)) {
                    *child_index += 1;
                    *next = child.source.as_deref();
                }
                while let Some(child) = next.filter(|child| child.logical_parent == Some(index)) {
                    self.pending.push_back(Node::Chain {
                        node: child,
                        index: *child_index,
                        cursor: Some(cursor),
                    });
                    *child_index += 1;
                    *next = child.source.as_deref();
                }
            }
        }
    }
}

impl<'a> Iterator for Errors<'a> {
    type Item = DisplaySource<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        // Defer expansion until the caller asks for another error, so a match need not inspect any of its causes.
        if let Some(previous) = self.previous.take() {
            self.children(previous);
        }
        let node = self.root.take().or_else(|| self.pending.pop_front())?;
        self.previous = Some(node);
        Some(node.display())
    }
}

impl crate::exn::Frame {
    pub(crate) fn probable_cause_inner(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Node::Frame(self).probable_cause()
    }

    pub(crate) fn iter_errors_with_locations(&self) -> impl Iterator<Item = DisplaySource<'_>> + '_ {
        Errors::new(Node::Frame(self)).filter(|source| !is_transparent_marker(source.error))
    }
}

#[cfg(any(feature = "tree-error", not(feature = "auto-chain-error")))]
mod _impl {
    use crate::{Error, Exn};
    use std::fmt::Formatter;

    /// Utilities
    impl Error {
        /// Return the error stored at this error boundary.
        ///
        /// This can be a classification marker hidden from [`Self::iter_errors()`], and is distinct from
        /// [`Self::probable_cause()`].
        pub fn error(&self) -> &(dyn std::error::Error + 'static) {
            self.inner.frame().error()
        }

        pub(super) fn iter_root(&self) -> super::Node<'_> {
            super::Node::Frame(self.inner.frame())
        }
    }

    pub(crate) enum Inner {
        ExnAsError(Box<crate::exn::Frame>),
        Exn(Box<crate::exn::Frame>),
    }

    impl Inner {
        pub(crate) fn frame(&self) -> &crate::exn::Frame {
            match self {
                Inner::ExnAsError(f) | Inner::Exn(f) => f,
            }
        }
    }

    impl Error {
        /// Create a new instance representing the given `error`.
        #[track_caller]
        pub fn from_error(error: impl std::error::Error + Send + Sync + 'static) -> Self {
            Error {
                inner: Inner::ExnAsError(Exn::new(error).into()),
            }
        }

        /// Create a new instance representing an already boxed `error`.
        #[track_caller]
        pub fn from_boxed(error: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
            Self::from_error(crate::exn::Untyped::from_boxed(error))
        }
    }

    impl std::fmt::Display for Error {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            match &self.inner {
                Inner::ExnAsError(err) => std::fmt::Display::fmt(err.error(), f),
                Inner::Exn(frame) => std::fmt::Display::fmt(frame, f),
            }
        }
    }

    impl std::fmt::Debug for Error {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            match &self.inner {
                Inner::ExnAsError(err) => std::fmt::Debug::fmt(err.error(), f),
                Inner::Exn(frame) => std::fmt::Debug::fmt(frame, f),
            }
        }
    }

    impl std::error::Error for Error {
        /// Return the first source of an [Exn] error, or the source of a boxed error.
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            match &self.inner {
                Inner::ExnAsError(frame) | Inner::Exn(frame) => {
                    let error = frame.error();
                    (!error.is::<Error>())
                        .then(|| super::native_source(error))
                        .flatten()
                        .or_else(|| frame.children().first().map(|frame| frame.error() as _))
                }
            }
        }
    }

    impl<E> From<Exn<E>> for Error
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        fn from(err: Exn<E>) -> Self {
            Error {
                inner: Inner::Exn(err.into()),
            }
        }
    }
}
#[cfg(any(feature = "tree-error", not(feature = "auto-chain-error")))]
pub(super) use _impl::Inner;

#[cfg(all(feature = "auto-chain-error", not(feature = "tree-error")))]
mod _impl {
    use crate::{Error, Exn};
    use std::fmt::Formatter;

    /// Utilities
    impl Error {
        /// Return the error stored at this error boundary.
        ///
        /// This can be a classification marker hidden from [`Self::iter_errors()`], and is distinct from
        /// [`Self::probable_cause()`].
        pub fn error(&self) -> &(dyn std::error::Error + 'static) {
            self.inner.err.error()
        }

        pub(super) fn iter_root(&self) -> super::Node<'_> {
            super::Node::Chain {
                node: &self.inner,
                index: 0,
                cursor: None,
            }
        }
    }

    impl Error {
        /// Create a new instance representing the given `error`.
        #[track_caller]
        pub fn from_error(error: impl std::error::Error + Send + Sync + 'static) -> Self {
            Error {
                inner: Exn::new(error).into_chain(),
            }
        }

        /// Create a new instance representing an already boxed `error`.
        #[track_caller]
        pub fn from_boxed(error: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
            Self::from_error(crate::exn::Untyped::from_boxed(error))
        }
    }

    impl std::fmt::Display for Error {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            if super::is_transparent_marker(self.error())
                && let Some(diagnostic) = self.iter_errors_with_locations().next()
            {
                return std::fmt::Display::fmt(&diagnostic, f);
            }
            std::fmt::Display::fmt(&self.inner, f)
        }
    }

    impl std::fmt::Debug for Error {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            if super::is_transparent_marker(self.error())
                && let Some(diagnostic) = self.iter_errors().next()
            {
                return std::fmt::Debug::fmt(diagnostic, f);
            }
            std::fmt::Debug::fmt(&self.inner, f)
        }
    }

    impl std::error::Error for Error {
        /// Return the first source of an [Exn] error, or the source of a boxed error.
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            self.inner.source()
        }
    }

    impl<E> From<Exn<E>> for Error
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        fn from(err: Exn<E>) -> Self {
            Error {
                inner: err.into_chain(),
            }
        }
    }
}

/// Retain I/O payloads, which `std::io::Error::source()` skips even when they carry a classification or an error tree.
pub(crate) fn native_source<'a>(
    err: &'a (dyn std::error::Error + 'static),
) -> Option<&'a (dyn std::error::Error + 'static)> {
    match err.downcast_ref::<std::io::Error>() {
        Some(err) => err.get_ref().map(|err| err as _),
        None => err.source(),
    }
}

pub(crate) fn is_transparent_marker(mut error: &(dyn std::error::Error + 'static)) -> bool {
    while let Some(nested) = error.downcast_ref::<crate::Error>() {
        error = nested.error();
    }
    error.is::<crate::ClassificationMarker>()
}