-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
94 lines (82 loc) · 2.98 KB
/
Copy patherror.rs
File metadata and controls
94 lines (82 loc) · 2.98 KB
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
use serde::{ser::SerializeStruct, ser::Serializer, Serialize};
use crate::models::{NativeCallError, NativeCallFailureCode};
pub type Result<T> = std::result::Result<T, Error>;
/// Command errors: the bounded failure vocabulary plus the bridge-level
/// `timeout` (a native invocation exceeded its time bound).
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("the native call bridge did not respond in time")]
Timeout,
#[error("{0}")]
Native(NativeCallError),
}
impl From<NativeCallError> for Error {
fn from(error: NativeCallError) -> Self {
Self::Native(error)
}
}
impl Error {
pub(crate) fn failure(code: NativeCallFailureCode) -> Self {
Self::Native(NativeCallError::from_code(code))
}
pub fn code(&self) -> &'static str {
match self {
Self::Timeout => "timeout",
Self::Native(error) => error.code.code(),
}
}
pub fn message(&self) -> &'static str {
match self {
Self::Timeout => "the native call bridge did not respond in time",
Self::Native(error) => error.code.message(),
}
}
}
#[cfg(mobile)]
impl From<tauri::plugin::mobile::PluginInvokeError> for Error {
fn from(_: tauri::plugin::mobile::PluginInvokeError) -> Self {
// Setup-time failure: the native bridge was never registered, so the
// native call APIs are unavailable.
Self::failure(NativeCallFailureCode::Unavailable)
}
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut error = serializer.serialize_struct("Error", 2)?;
error.serialize_field("code", self.code())?;
error.serialize_field("message", self.message())?;
error.end()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn errors_serialize_to_the_bounded_vocabulary_with_static_messages() {
assert_eq!(
serde_json::to_value(Error::Timeout).unwrap(),
serde_json::json!({
"code": "timeout",
"message": "the native call bridge did not respond in time"
})
);
for (code, wire) in [
(NativeCallFailureCode::InvalidRequest, "invalid_request"),
(NativeCallFailureCode::Busy, "busy"),
(NativeCallFailureCode::PermissionDenied, "permission_denied"),
(NativeCallFailureCode::ConnectFailed, "connect_failed"),
(NativeCallFailureCode::MediaFailed, "media_failed"),
(NativeCallFailureCode::Disconnected, "disconnected"),
(NativeCallFailureCode::Cancelled, "cancelled"),
(NativeCallFailureCode::Unavailable, "unavailable"),
(NativeCallFailureCode::Unexpected, "unexpected"),
] {
let error = serde_json::to_value(Error::failure(code)).unwrap();
assert_eq!(error["code"], wire);
assert_eq!(error["message"], code.message());
}
}
}