1use std::fmt;
10
11pub type Result<T, E = KatraError> = core::result::Result<T, E>;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum ErrorCategory {
18 InvalidArgument,
20 NotSupported,
22 OutOfBudget,
24 Io,
26 Protocol,
28 NotFound,
30 AlreadyExists,
32 Corrupt,
34 Timeout,
36 Sync,
38 FallbackUnavailable,
40 Internal,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
46pub enum KatraError {
47 InvalidArgument(&'static str),
49 NotSupported(String),
51 OutOfBudget {
53 resource: &'static str,
55 requested: u64,
57 available: u64,
59 },
60 Io(std::io::ErrorKind, String),
62 Protocol(String),
64 NotFound(String),
66 AlreadyExists(String),
68 Corrupt(String),
70 Timeout {
72 what: &'static str,
74 timeout_ns: u64,
76 },
77 Sync(String),
79 FallbackUnavailable(&'static str),
81 Internal(&'static str),
83}
84
85impl KatraError {
86 pub fn category(&self) -> ErrorCategory {
88 match self {
89 KatraError::InvalidArgument(_) => ErrorCategory::InvalidArgument,
90 KatraError::NotSupported(_) => ErrorCategory::NotSupported,
91 KatraError::OutOfBudget { .. } => ErrorCategory::OutOfBudget,
92 KatraError::Io(..) => ErrorCategory::Io,
93 KatraError::Protocol(_) => ErrorCategory::Protocol,
94 KatraError::NotFound(_) => ErrorCategory::NotFound,
95 KatraError::AlreadyExists(_) => ErrorCategory::AlreadyExists,
96 KatraError::Corrupt(_) => ErrorCategory::Corrupt,
97 KatraError::Timeout { .. } => ErrorCategory::Timeout,
98 KatraError::Sync(_) => ErrorCategory::Sync,
99 KatraError::FallbackUnavailable(_) => ErrorCategory::FallbackUnavailable,
100 KatraError::Internal(_) => ErrorCategory::Internal,
101 }
102 }
103}
104
105impl fmt::Display for KatraError {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 match self {
108 KatraError::InvalidArgument(a) => write!(f, "invalid argument: {a}"),
109 KatraError::NotSupported(c) => write!(f, "not supported: {c}"),
110 KatraError::OutOfBudget { resource, requested, available } => write!(
111 f,
112 "budget exceeded for {resource}: requested {requested}, available {available}"
113 ),
114 KatraError::Io(kind, msg) => write!(f, "io error ({kind}): {msg}"),
115 KatraError::Protocol(msg) => write!(f, "protocol violation: {msg}"),
116 KatraError::NotFound(msg) => write!(f, "not found: {msg}"),
117 KatraError::AlreadyExists(msg) => write!(f, "already exists: {msg}"),
118 KatraError::Corrupt(msg) => write!(f, "corrupt data: {msg}"),
119 KatraError::Timeout { what, timeout_ns } => {
120 write!(f, "timeout on {what} after {timeout_ns} ns")
121 }
122 KatraError::Sync(msg) => write!(f, "synchronization violation: {msg}"),
123 KatraError::FallbackUnavailable(s) => write!(f, "fallback unavailable: {s}"),
124 KatraError::Internal(msg) => write!(f, "internal error: {msg}"),
125 }
126 }
127}
128
129impl std::error::Error for KatraError {}
130
131impl From<std::io::Error> for KatraError {
132 fn from(e: std::io::Error) -> Self {
133 KatraError::Io(e.kind(), e.to_string())
134 }
135}