Skip to content

Commit 13d3bb9

Browse files
authored
don't panic on UTF8 error (#15)
* don't panic on UTF8 error * Add missing dyn for trait objects * Clean up error handling on UTF-8 decoding * import RedisError
1 parent d110395 commit 13d3bb9

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

examples/data_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern crate redismodule;
33

44
use redismodule::native_types::RedisType;
5-
use redismodule::{Context, NextArg, RedisResult};
5+
use redismodule::{Context, NextArg, RedisResult, RedisError};
66

77
#[derive(Debug)]
88
struct MyType {

examples/hello_redisearch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern crate redismodule;
33

44
use redismodule::redisearch;
5-
use redismodule::{Context, NextArg, RedisResult};
5+
use redismodule::{Context, NextArg, RedisResult, RedisError};
66

77
fn hello_redisearch(_: &Context, args: Vec<String>) -> RedisResult {
88
let mut args = args.into_iter().skip(1);

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl error::Error for Error {
5151
}
5252
}
5353

54-
fn cause(&self) -> Option<&error::Error> {
54+
fn cause(&self) -> Option<&dyn error::Error> {
5555
match *self {
5656
// N.B. Both of these implicitly cast `err` from their concrete
5757
// types (either `&io::Error` or `&num::ParseIntError`)
@@ -88,7 +88,7 @@ impl<'a> error::Error for GenericError {
8888
self.message.as_str()
8989
}
9090

91-
fn cause(&self) -> Option<&error::Error> {
91+
fn cause(&self) -> Option<&dyn error::Error> {
9292
None
9393
}
9494
}

src/macros.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ macro_rules! redis_command {
1313
) -> c_int {
1414
let context = Context::new(ctx);
1515

16-
let args: Vec<String> = unsafe { slice::from_raw_parts(argv, argc as usize) }
17-
.into_iter()
18-
.map(|a| {
19-
RedisString::from_ptr(*a)
20-
.expect("UTF8 encoding error in handler args")
21-
.to_string()
22-
})
23-
.collect();
16+
let args_decoded: Result<Vec<_>, RedisError> =
17+
unsafe { slice::from_raw_parts(argv, argc as usize) }
18+
.into_iter()
19+
.map(|&arg| {
20+
RedisString::from_ptr(arg)
21+
.map(|v| v.to_owned())
22+
.map_err(|_| RedisError::Str("UTF8 encoding error in handler args"))
23+
})
24+
.collect();
25+
26+
let response = args_decoded
27+
.map(|args| $command_handler(&context, args))
28+
.unwrap_or_else(|e| Err(e));
2429

25-
let response = $command_handler(&context, args);
2630
context.reply(response) as c_int
2731
}
2832
/////////////////////

0 commit comments

Comments
 (0)