forked from RedisJSON/RedisJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.rs
138 lines (116 loc) · 3.77 KB
/
error.rs
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
use jsonpath_lib::select::JsonPathError;
use redis_module::RedisError;
use std::num::ParseIntError;
#[derive(Debug)]
pub struct Error {
pub msg: String,
}
impl From<String> for Error {
fn from(e: String) -> Self {
Error { msg: e }
}
}
impl From<redis_module::error::GenericError> for Error {
fn from(err: redis_module::error::GenericError) -> Error {
err.to_string().into()
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Error {
err.to_string().into()
}
}
impl From<ParseIntError> for Error {
fn from(err: ParseIntError) -> Error {
err.to_string().into()
}
}
impl From<redis_module::error::Error> for Error {
fn from(e: redis_module::error::Error) -> Self {
match e {
redis_module::error::Error::Generic(err) => err.into(),
redis_module::error::Error::FromUtf8(err) => err.into(),
redis_module::error::Error::ParseInt(err) => err.into(),
}
}
}
impl From<RedisError> for Error {
fn from(e: RedisError) -> Self {
Self::from(format!("ERR {}", e))
}
}
impl From<&str> for Error {
fn from(e: &str) -> Self {
Error { msg: e.to_string() }
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error { msg: e.to_string() }
}
}
impl From<JsonPathError> for Error {
fn from(e: JsonPathError) -> Self {
Error {
msg: format!("JSON Path error: {:?}", e).replace("\n", "\\n"),
}
}
}
impl From<Error> for redis_module::RedisError {
fn from(e: Error) -> Self {
redis_module::RedisError::String(e.msg)
}
}
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
#[test]
fn test_from_string() {
let err: Error = String::from("error from String").into();
assert_eq!(err.msg, "error from String");
}
#[test]
fn test_from_generic_error() {
let err: Error = redis_module::error::GenericError::new("error from GenericError").into();
assert_eq!(err.msg, "Store error: error from GenericError");
}
#[test]
fn test_from_utf8_error() {
let err: Error = String::from_utf8(vec![128, 0, 0]).unwrap_err().into();
assert_eq!(err.msg, "invalid utf-8 sequence of 1 bytes from index 0");
}
#[test]
fn test_from_parse_int_error() {
let err: Error = i32::from_str_radix("a12", 10).unwrap_err().into();
assert_eq!(err.msg, "invalid digit found in string");
}
#[test]
fn test_from_redis_error() {
let err: Error = redis_module::error::Error::generic("error from Generic").into();
assert_eq!(err.msg, "Store error: error from Generic");
let utf8_err: redis_module::error::Error =
String::from_utf8(vec![128, 0, 0]).unwrap_err().into();
let err: Error = utf8_err.into();
assert_eq!(err.msg, "invalid utf-8 sequence of 1 bytes from index 0");
let parse_int_error: redis_module::error::Error =
i32::from_str_radix("a12", 10).unwrap_err().into();
let err: Error = parse_int_error.into();
assert_eq!(err.msg, "invalid digit found in string");
}
// #[test]
// fn test_from_serde_json_error() {
// let err : Error = serde_json::Error "error from str".into();
// assert_eq!(err.msg, "error from str");
// }
#[test]
fn test_from_jsonpath_error() {
let err: Error = JsonPathError::Path("JsonPathError".to_string()).into();
assert_eq!(err.msg, "JSON Path error: path error: \\nJsonPathError\\n");
}
#[test]
fn test_from_str() {
let err: Error = "error from str".into();
assert_eq!(err.msg, "error from str");
}
}