Skip to main content

harmos_storage/
error.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16// Standard Library Imports
17
18// Third Party Imports
19
20// Local Imports
21
22/* === Constants === */
23
24/* === Macros === */
25
26/* === Type Aliases === */
27
28pub type Result<T> = std::result::Result<T, Error>;
29
30/* === Structs === */
31
32/* === Enums === */
33
34#[derive(Debug, thiserror::Error)]
35pub enum Error {
36    #[error("operation `{operation}` failed")]
37    OperationFailed {
38        operation: String,
39
40        #[source]
41        source: std::sync::Arc<dyn std::error::Error + Send + Sync>,
42    },
43
44    #[error("{domain} is not ready")]
45    NotReady { domain: &'static str },
46
47    #[error("persistence failed: {source}")]
48    Persistence {
49        #[source]
50        source: std::sync::Arc<Error>,
51    },
52
53    #[error("{domain} admission is closed")]
54    Closing { domain: &'static str },
55
56    #[error("{domain} `{name}` is already registered")]
57    AlreadyRegistered { domain: &'static str, name: String },
58
59    #[error("backend {operation} failed ({kind:?}): {source}")]
60    Backend {
61        operation: &'static str,
62        kind: BackendFailure,
63
64        #[source]
65        source: Box<dyn std::error::Error + Send + Sync>,
66    },
67
68    #[error("invalid {field}: {reason}")]
69    InvalidInput { field: &'static str, reason: &'static str },
70
71    #[error("{resource} capacity is exhausted")]
72    ResourceExhausted { resource: &'static str },
73
74    #[error("retained data begins at {available}, but {requested} was requested")]
75    Gap { requested: u64, available: u64 },
76
77    #[error(transparent)]
78    Domain(#[from] harmos_domain::Error),
79}
80
81#[derive(Clone, Copy, Debug, PartialEq, Eq)]
82pub enum BackendFailure {
83    Transient,
84    Permanent,
85    Corrupt,
86    Conflict,
87    Unsupported,
88}
89
90/* === Traits === */
91
92/* === Implementations === */
93
94impl Error {
95    pub fn backend_failure(&self) -> Option<BackendFailure> {
96        match self {
97            Self::Backend { kind, .. } => Some(*kind),
98            Self::Persistence { source } => source.backend_failure(),
99            _ => None,
100        }
101    }
102
103    pub fn backend(operation: &'static str, kind: BackendFailure, source: impl std::error::Error + Send + Sync + 'static) -> Self {
104        Self::Backend { operation, kind, source: Box::new(source) }
105    }
106}
107
108/* === Public Functions === */
109
110/* === Private Functions === */
111
112/* === Tests === */