Skip to main content

qubit_dcl/double_checked/
executor_error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Executor Error
10//!
11//! Provides executor error types for the double-checked lock executor.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use std::error::Error;
18use std::fmt;
19
20/// Executor error types
21///
22/// Defines various error conditions that can occur during executor
23/// operation, including task execution errors, prepare failures, prepare
24/// commit failures, and prepare rollback failures.
25///
26/// # Type Parameters
27///
28/// * `E` - The original error type from task execution
29///
30/// # Examples
31///
32/// ```rust
33/// use qubit_dcl::double_checked::ExecutorError;
34///
35/// let error: ExecutorError<String> =
36///     ExecutorError::TaskFailed("task failed".to_string());
37/// println!("Error: {}", error);
38///
39/// let error_with_msg: ExecutorError<String> =
40///     ExecutorError::PrepareFailed("Service is not running".to_string());
41/// println!("Error: {}", error_with_msg);
42/// ```
43///
44/// # Author
45///
46/// Haixing Hu
47///
48#[derive(Debug)]
49pub enum ExecutorError<E>
50where
51    E: fmt::Display,
52{
53    /// Task execution failed with original error
54    TaskFailed(E),
55
56    /// Preparation action failed
57    PrepareFailed(String),
58
59    /// Commit action for a successfully completed prepare action failed.
60    PrepareCommitFailed(String),
61
62    /// Rollback action for a successfully completed prepare action failed.
63    PrepareRollbackFailed {
64        /// The original error that triggered the rollback
65        original: String,
66        /// The error that occurred during prepare rollback
67        rollback: String,
68    },
69
70    /// Lock poisoned error
71    LockPoisoned(String),
72}
73
74impl<E> fmt::Display for ExecutorError<E>
75where
76    E: fmt::Display,
77{
78    /// Formats this executor error for user-facing diagnostics.
79    ///
80    /// # Parameters
81    ///
82    /// * `f` - Formatter receiving the human-readable error text.
83    ///
84    /// # Returns
85    ///
86    /// [`fmt::Result`] from writing the formatted error text.
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        match self {
89            ExecutorError::TaskFailed(e) => {
90                write!(f, "Task execution failed: {}", e)
91            }
92            ExecutorError::PrepareFailed(msg) => {
93                write!(f, "Preparation action failed: {}", msg)
94            }
95            ExecutorError::PrepareCommitFailed(msg) => {
96                write!(f, "Prepare commit action failed: {}", msg)
97            }
98            ExecutorError::PrepareRollbackFailed { original, rollback } => {
99                write!(
100                    f,
101                    "Prepare rollback failed: original error = {}, rollback error = {}",
102                    original, rollback
103                )
104            }
105            ExecutorError::LockPoisoned(msg) => {
106                write!(f, "Lock poisoned: {}", msg)
107            }
108        }
109    }
110}
111
112impl<E> Error for ExecutorError<E> where E: fmt::Display + fmt::Debug {}