Skip to main content

qubit_retry/error/
attempt_executor_error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Attempt executor failure information.
11//!
12
13use std::error::Error;
14use std::fmt;
15
16use serde::{
17    Deserialize,
18    Serialize,
19};
20
21/// Failure produced by the retry executor before an attempt can run normally.
22///
23/// This type is used for infrastructure failures such as failing to spawn a
24/// worker thread for [`crate::Retry::run_in_worker`].
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26pub struct AttemptExecutorError {
27    /// Human-readable executor failure message.
28    message: Box<str>,
29}
30
31impl AttemptExecutorError {
32    /// Creates an executor failure from a message.
33    ///
34    /// # Parameters
35    /// - `message`: Failure message to store.
36    ///
37    /// # Returns
38    /// An executor failure value.
39    #[inline]
40    pub fn new(message: &str) -> Self {
41        Self {
42            message: message.into(),
43        }
44    }
45
46    /// Creates an executor failure from a message and diagnostic detail.
47    ///
48    /// # Parameters
49    /// - `message`: High-level failure message to store.
50    /// - `detail`: Lower-level diagnostic detail appended to the message.
51    ///
52    /// # Returns
53    /// An executor failure value containing both the high-level message and
54    /// lower-level diagnostic detail.
55    #[inline]
56    pub fn with_context(message: &str, detail: &str) -> Self {
57        Self {
58            message: format!("{message}: {detail}").into_boxed_str(),
59        }
60    }
61
62    /// Returns the executor failure message.
63    ///
64    /// # Returns
65    /// Failure message text.
66    #[inline]
67    pub fn message(&self) -> &str {
68        &self.message
69    }
70}
71
72impl fmt::Display for AttemptExecutorError {
73    /// Formats the executor failure message.
74    ///
75    /// # Parameters
76    /// - `f`: Formatter provided by the standard formatting machinery.
77    ///
78    /// # Returns
79    /// Formatting result.
80    ///
81    /// # Errors
82    /// Returns a formatting error if the formatter rejects output.
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        f.write_str(&self.message)
85    }
86}
87
88impl Error for AttemptExecutorError {}