foundationdb/recipes/ranked_register/
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 the ranked register
9
10use crate::tuple::PackError;
11use crate::{FdbBindingError, FdbError, RetryableError};
12use std::fmt;
13
14/// Ranked register specific errors
15#[derive(Debug)]
16pub enum RankedRegisterError {
17    /// Database error
18    Fdb(FdbError),
19    /// Retry loop error
20    Binding(FdbBindingError),
21    /// Serialization error
22    PackError(PackError),
23    /// Invalid state encountered in the register
24    InvalidState(String),
25    /// The requested raw value exceeds this handle's configured limit.
26    ///
27    /// This is a non-retryable caller error. The limit is local configuration
28    /// and is never persisted in FoundationDB.
29    ValueTooLarge {
30        /// Requested raw value size in bytes.
31        value_size: usize,
32        /// Configured maximum raw value size in bytes.
33        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
83/// The `source()` chain exposes the wrapped `FdbError`, so the default
84/// `retry_decision` makes this error retry-transparent in `db.run`.
85impl RetryableError for RankedRegisterError {}
86
87/// Result type for ranked register operations
88pub type Result<T> = std::result::Result<T, RankedRegisterError>;