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::{
12    error::Error,
13    fmt,
14};
15
16use crate::{
17    Event,
18    error::ReporterError,
19};
20
21/// Failure while delivering one complete Event to a reporter.
22#[derive(Clone, Debug)]
23pub struct DeliveryError {
24    event: Box<Event>,
25    source: ReporterError,
26}
27
28impl DeliveryError {
29    /// Creates a delivery error retaining the failed Event and sink error.
30    pub(crate) fn new(event: Event, source: ReporterError) -> Self {
31        Self {
32            event: Box::new(event),
33            source,
34        }
35    }
36
37    /// Returns the complete Event whose delivery failed.
38    #[must_use]
39    pub const fn event(&self) -> &Event {
40        &self.event
41    }
42
43    /// Returns the original reporter error.
44    #[must_use]
45    pub const fn reporter_error(&self) -> &ReporterError {
46        &self.source
47    }
48
49    /// Consumes the error and returns its failed Event.
50    #[must_use]
51    pub fn into_event(self) -> Event {
52        *self.event
53    }
54
55    /// Consumes the error and returns its reporter error.
56    #[must_use]
57    pub fn into_reporter_error(self) -> ReporterError {
58        self.source
59    }
60
61    /// Consumes the error and returns the Event with its reporter error.
62    #[must_use]
63    pub fn into_parts(self) -> (Event, ReporterError) {
64        (*self.event, self.source)
65    }
66}
67
68impl fmt::Display for DeliveryError {
69    /// Formats the failed Event identity and reporter error.
70    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
71        write!(
72            formatter,
73            "delivery of {} event for operation {} sequence {} failed: {}",
74            self.event.phase().as_str(),
75            self.event.operation_id(),
76            self.event.sequence(),
77            self.source,
78        )
79    }
80}
81
82impl Error for DeliveryError {
83    /// Returns the reporter error as the cause.
84    fn source(&self) -> Option<&(dyn Error + 'static)> {
85        Some(&self.source)
86    }
87}