foundationdb/recipes/leader_election/
errors.rs

1// Copyright 2024 foundationdb-rs developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! Error types for leader election
9//!
10//! Defines the error hierarchy for leader election operations,
11//! including election-specific errors and conversions from underlying errors.
12
13use crate::tuple::PackError;
14use crate::FdbError;
15use std::fmt;
16
17/// Leader election specific errors
18///
19/// Represents all possible error conditions that can occur during
20/// leader election operations.
21#[derive(Debug)]
22pub enum LeaderElectionError {
23    /// Election is currently disabled
24    ///
25    /// Returned when attempting election operations while the election
26    /// system is administratively disabled
27    ElectionDisabled,
28    /// Process not found
29    ///
30    /// The specified process UUID doesn't exist in the election registry
31    ProcessNotFound(String),
32    /// Global configuration not initialized
33    ///
34    /// The election system hasn't been initialized with `initialize()`
35    NotInitialized,
36    /// Invalid state
37    ///
38    /// The election system is in an inconsistent or unexpected state
39    InvalidState(String),
40    /// Database error
41    ///
42    /// An underlying FoundationDB error occurred
43    Fdb(FdbError),
44    /// Serialization error
45    ///
46    /// Failed to pack/unpack data using the tuple layer
47    PackError(PackError),
48    /// Candidate not registered
49    ///
50    /// The process attempted to claim leadership without being registered as a candidate
51    UnregisteredCandidate,
52}
53
54impl fmt::Display for LeaderElectionError {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            Self::ElectionDisabled => write!(f, "Election is currently disabled"),
58            Self::ProcessNotFound(id) => write!(f, "Process not found: {id}"),
59            Self::NotInitialized => write!(f, "Global configuration not initialized"),
60            Self::InvalidState(msg) => write!(f, "Invalid state: {msg}"),
61            Self::Fdb(e) => write!(f, "Database error: {e}"),
62            Self::PackError(e) => write!(f, "Pack error: {e:?}"),
63            Self::UnregisteredCandidate => write!(f, "Candidate not registered"),
64        }
65    }
66}
67
68impl std::error::Error for LeaderElectionError {}
69
70impl From<FdbError> for LeaderElectionError {
71    fn from(error: FdbError) -> Self {
72        Self::Fdb(error)
73    }
74}
75
76impl From<PackError> for LeaderElectionError {
77    fn from(error: PackError) -> Self {
78        Self::PackError(error)
79    }
80}
81
82/// Result type for leader election operations
83///
84/// Convenience type alias for Results that may fail with LeaderElectionError
85pub type Result<T> = std::result::Result<T, LeaderElectionError>;