foundationdb/recipes/ranked_register/
errors.rs1use crate::tuple::PackError;
11use crate::{FdbBindingError, FdbError, RetryableError};
12use std::fmt;
13
14#[derive(Debug)]
16pub enum RankedRegisterError {
17 Fdb(FdbError),
19 Binding(FdbBindingError),
21 PackError(PackError),
23 InvalidState(String),
25 ValueTooLarge {
30 value_size: usize,
32 limit: usize,
34 },
35}
36
37impl fmt::Display for RankedRegisterError {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 match self {
40 Self::Fdb(e) => write!(f, "Database error: {e}"),
41 Self::Binding(e) => write!(f, "Retry loop error: {e}"),
42 Self::PackError(e) => write!(f, "Pack error: {e:?}"),
43 Self::InvalidState(msg) => write!(f, "Invalid state: {msg}"),
44 Self::ValueTooLarge { value_size, limit } => {
45 write!(
46 f,
47 "Register value is {value_size} bytes, above the {limit}-byte limit"
48 )
49 }
50 }
51 }
52}
53
54impl std::error::Error for RankedRegisterError {
55 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56 match self {
57 Self::Fdb(e) => Some(e),
58 Self::Binding(e) => Some(e),
59 Self::PackError(e) => Some(e),
60 Self::InvalidState(_) | Self::ValueTooLarge { .. } => None,
61 }
62 }
63}
64
65impl From<FdbError> for RankedRegisterError {
66 fn from(error: FdbError) -> Self {
67 Self::Fdb(error)
68 }
69}
70
71impl From<FdbBindingError> for RankedRegisterError {
72 fn from(error: FdbBindingError) -> Self {
73 Self::Binding(error)
74 }
75}
76
77impl From<PackError> for RankedRegisterError {
78 fn from(error: PackError) -> Self {
79 Self::PackError(error)
80 }
81}
82
83impl RetryableError for RankedRegisterError {}
86
87pub type Result<T> = std::result::Result<T, RankedRegisterError>;