Skip to content

Commit f4d0020

Browse files
committed
MOD-14672 RDB load - return error on failure
1 parent 7d67dd1 commit f4d0020

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

redis_json/src/backward.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,25 @@ enum NodeType {
3030
// N_BINARY = 0x200
3131
}
3232

33-
impl From<u64> for NodeType {
34-
fn from(n: u64) -> Self {
33+
impl TryFrom<u64> for NodeType {
34+
type Error = RedisError;
35+
fn try_from(n: u64) -> Result<Self, Self::Error> {
3536
match n {
36-
0x1u64 => Self::Null,
37-
0x2u64 => Self::String,
38-
0x4u64 => Self::Number,
39-
0x8u64 => Self::Integer,
40-
0x10u64 => Self::Boolean,
41-
0x20u64 => Self::Dict,
42-
0x40u64 => Self::Array,
43-
0x80u64 => Self::KeyVal,
44-
_ => panic!("Can't load old RedisJSON RDB1"),
37+
0x1u64 => Ok(Self::Null),
38+
0x2u64 => Ok(Self::String),
39+
0x4u64 => Ok(Self::Number),
40+
0x8u64 => Ok(Self::Integer),
41+
0x10u64 => Ok(Self::Boolean),
42+
0x20u64 => Ok(Self::Dict),
43+
0x40u64 => Ok(Self::Array),
44+
0x80u64 => Ok(Self::KeyVal),
45+
_ => Err(RedisError::Str("Can't load old RedisJSON RDB1")),
4546
}
4647
}
4748
}
4849

4950
pub fn json_rdb_load(rdb: *mut raw::RedisModuleIO) -> RedisResult<Value> {
50-
let node_type = raw::load_unsigned(rdb)?.into();
51+
let node_type = raw::load_unsigned(rdb)?.try_into()?;
5152
match node_type {
5253
NodeType::Null => Ok(Value::Null),
5354
NodeType::Boolean => {
@@ -72,7 +73,7 @@ pub fn json_rdb_load(rdb: *mut raw::RedisModuleIO) -> RedisResult<Value> {
7273
let len = raw::load_unsigned(rdb)?;
7374
let mut m = Map::with_capacity(len as usize);
7475
for _ in 0..len {
75-
let t: NodeType = raw::load_unsigned(rdb)?.into();
76+
let t: NodeType = raw::load_unsigned(rdb)?.try_into()?;
7677
if t != NodeType::KeyVal {
7778
return Err(RedisError::Str("Can't load old RedisJSON RDB"));
7879
}

redis_json/src/redisjson.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ pub mod type_methods {
213213
let v = backward::json_rdb_load(rdb)?;
214214

215215
let mut out = serde_json::Serializer::new(Vec::new());
216-
v.serialize(&mut out).unwrap();
217-
String::from_utf8(out.into_inner()).unwrap()
216+
v.serialize(&mut out)?;
217+
String::from_utf8(out.into_inner())?
218218
}
219219
2 => {
220220
let data = raw::load_string(rdb)?;
@@ -231,7 +231,11 @@ pub mod type_methods {
231231
let data = raw::load_string(rdb)?;
232232
data.try_as_str()?.to_string()
233233
}
234-
_ => panic!("Can't load old RedisJSON RDB"),
234+
v => {
235+
return Err(RedisError::String(format!(
236+
"Can't load old RedisJSON RDB: {v}"
237+
)))
238+
}
235239
})
236240
}
237241

0 commit comments

Comments
 (0)