Skip to main content

qubit_progress/error/
delivery_error.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Errors associated with one concrete Event delivery attempt.
9// qubit-style: allow source-test-pair
10
11use std::error::Error;
12use std::fmt;
13
14use crate::Event;
15use crate::error::ReporterError;
16
17/// Failure while delivering one complete Event to a reporter.
18#[derive(Clone, Debug)]
19pub struct DeliveryError {
20    event: Box<Event>,
21    source: ReporterError,
22}
23
24impl DeliveryError {
25    /// Creates a delivery error retaining the failed Event and sink error.
26    pub(crate) fn new(event: Event, source: ReporterError) -> Self {
27        Self {
28            event: Box::new(event),
29            source,
30        }
31    }
32
33    /// Returns the complete Event whose delivery failed.
34    #[must_use]
35    pub const fn event(&self) -> &Event {
36        &self.event
37    }
38
39    /// Returns the original reporter error.
40    #[must_use]
41    pub const fn reporter_error(&self) -> &ReporterError {
42        &self.source
43    }
44
45    /// Consumes the error and returns its failed Event.
46    #[must_use]
47    pub fn into_event(self) -> Event {
48        *self.event
49    }
50
51    /// Consumes the error and returns its reporter error.
52    #[must_use]
53    pub fn into_reporter_error(self) -> ReporterError {
54        self.source
55    }
56
57    /// Consumes the error and returns the Event with its reporter error.
58    #[must_use]
59    pub fn into_parts(self) -> (Event, ReporterError) {
60        (*self.event, self.source)
61    }
62}
63
64impl fmt::Display for DeliveryError {
65    /// Formats the failed Event identity and reporter error.
66    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
67        write!(
68            formatter,
69            "delivery of {} event for operation {} sequence {} failed: {}",
70            self.event.phase().as_str(),
71            self.event.operation_id(),
72            self.event.sequence(),
73            self.source,
74        )
75    }
76}
77
78impl Error for DeliveryError {
79    /// Returns the reporter error as the cause.
80    fn source(&self) -> Option<&(dyn Error + 'static)> {
81        Some(&self.source)
82    }
83}