1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::error::Error as StdError;
use std::fmt::{Display, Formatter};
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InnerErrorCode {
/// Unknown/unclassified error.
Unknown = -1,
/// Success (non-error sentinel).
Success = 0,
/// Runtime creation failed.
RuntimeCreationFailedError = 101,
/// Required parameter is empty or invalid.
ParameterEmpty = 102,
/// The same file/task is already queued or running.
DuplicateTaskError = 103,
/// Failed to enqueue task.
EnqueueError = 104,
/// Local I/O operation failed.
IoError = 105,
/// HTTP request/response operation failed.
HttpError = 106,
/// Client has already been closed and can no longer accept operations.
ClientClosed = 107,
/// Unknown task ID in control API.
TaskNotFound = 108,
/// HTTP response status is not expected.
ResponseStatusError = 109,
/// `Content-Length` from HEAD is missing or invalid.
MissingOrInvalidContentLengthFromHead = 110,
/// Failed to send command to scheduler thread.
CommandSendFailed = 111,
/// Command response channel closed unexpectedly.
CommandResponseFailed = 112,
/// Failed to parse response payload (for example JSON).
ResponseParseError = 113,
/// Invalid HTTP range semantics or headers.
InvalidRange = 114,
/// Local file does not exist.
FileNotFound = 115,
/// File checksum/signature does not match expected value.
ChecksumMismatch = 116,
/// Current task state does not allow requested operation.
InvalidTaskState = 117,
/// Internal lock is poisoned.
LockPoisoned = 118,
/// Failed to build internal HTTP client.
HttpClientBuildFailed = 119,
/// Task was canceled before reaching `Complete`.
TaskCanceled = 120,
}
/// Library error type returned by most public APIs.
#[derive(Debug, Clone)]
pub struct MeowError {
/// Numeric error code, usually mapped from [`InnerErrorCode`].
code: i32,
/// Human-readable error message.
msg: String,
/// Optional chained source error.
source: Option<Arc<dyn StdError + Send + Sync>>,
}
impl MeowError {
/// Creates a new error with raw numeric code and message.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowError;
///
/// let err = MeowError::new(9999, "custom failure".to_string());
/// assert_eq!(err.code(), 9999);
/// ```
pub fn new(code: i32, msg: String) -> Self {
crate::log::emit_lazy(|| {
crate::log::Log::debug("error", format!("MeowError::new code={} msg={}", code, msg))
});
MeowError {
code,
msg,
source: None,
}
}
/// Returns numeric error code.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::{InnerErrorCode, MeowError};
///
/// let err = MeowError::from_code1(InnerErrorCode::ClientClosed);
/// assert_eq!(err.code(), InnerErrorCode::ClientClosed as i32);
/// ```
pub fn code(&self) -> i32 {
self.code
}
/// Returns the error message as a borrowed `&str`.
///
/// Borrowing avoids an allocation on every call; callers that need an
/// owned `String` can do `err.msg().to_owned()` explicitly.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::{InnerErrorCode, MeowError};
///
/// let err = MeowError::from_code_str(InnerErrorCode::InvalidRange, "bad range");
/// assert_eq!(err.msg(), "bad range");
/// ```
pub fn msg(&self) -> &str {
&self.msg
}
/// Creates an error from [`InnerErrorCode`] with empty message.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::{InnerErrorCode, MeowError};
///
/// let err = MeowError::from_code1(InnerErrorCode::ParameterEmpty);
/// assert_eq!(err.code(), InnerErrorCode::ParameterEmpty as i32);
/// ```
pub fn from_code1(code: InnerErrorCode) -> Self {
crate::log::emit_lazy(|| {
crate::log::Log::debug("error", format!("MeowError::from_code1 code={:?}", code))
});
MeowError {
code: code as i32,
msg: String::new(),
source: None,
}
}
/// Creates an error from [`InnerErrorCode`] and message.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::{InnerErrorCode, MeowError};
///
/// let err = MeowError::from_code(InnerErrorCode::EnqueueError, "enqueue failed".to_string());
/// assert_eq!(err.code(), InnerErrorCode::EnqueueError as i32);
/// ```
pub fn from_code(code: InnerErrorCode, msg: String) -> Self {
crate::log::emit_lazy(|| {
crate::log::Log::debug(
"error",
format!("MeowError::from_code code={:?} msg={}", code, msg),
)
});
MeowError {
code: code as i32,
msg,
source: None,
}
}
/// Creates an error from [`InnerErrorCode`] and `&str` message.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::{InnerErrorCode, MeowError};
///
/// let err = MeowError::from_code_str(InnerErrorCode::TaskNotFound, "unknown id");
/// assert_eq!(err.code(), InnerErrorCode::TaskNotFound as i32);
/// ```
pub fn from_code_str(code: InnerErrorCode, msg: &str) -> Self {
crate::log::emit_lazy(|| {
crate::log::Log::debug(
"error",
format!("MeowError::from_code_str code={:?} msg={}", code, msg),
)
});
MeowError {
code: code as i32,
msg: msg.to_string(),
source: None,
}
}
/// Creates an error with source chaining.
///
/// Use this helper to preserve original low-level errors.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::{InnerErrorCode, MeowError};
///
/// let source = std::io::Error::other("disk error");
/// let err = MeowError::from_source(InnerErrorCode::IoError, "upload failed", source);
/// assert_eq!(err.code(), InnerErrorCode::IoError as i32);
/// ```
pub fn from_source<E>(code: InnerErrorCode, msg: impl Into<String>, source: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
let msg = msg.into();
let source_preview = source.to_string();
crate::log::emit_lazy(|| {
crate::log::Log::debug(
"error",
format!(
"MeowError::from_source code={:?} msg={} source={}",
code, msg, source_preview
),
)
});
MeowError {
code: code as i32,
msg,
source: Some(Arc::new(source)),
}
}
}
impl PartialEq for MeowError {
fn eq(&self, other: &Self) -> bool {
self.code == other.code && self.msg == other.msg
}
}
impl Eq for MeowError {}
impl Display for MeowError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.msg.is_empty() {
write!(f, "MeowError(code={})", self.code)
} else {
write!(f, "MeowError(code={}, msg={})", self.code, self.msg)
}
}
}
impl StdError for MeowError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.source
.as_deref()
.map(|e| e as &(dyn StdError + 'static))
}
}
#[cfg(test)]
mod tests {
use super::{InnerErrorCode, MeowError};
#[test]
fn meow_error_display_contains_code_and_message() {
let err = MeowError::from_code_str(InnerErrorCode::InvalidRange, "bad range");
let s = format!("{err}");
assert!(s.contains("code="));
assert!(s.contains("bad range"));
}
#[test]
fn meow_error_source_is_accessible() {
let io = std::io::Error::other("disk io");
let err = MeowError::from_source(InnerErrorCode::IoError, "io failed", io);
assert!(std::error::Error::source(&err).is_some());
}
}