Skip to content

Commit 1e00c81

Browse files
committed
Handle query errors correctly
1 parent 92e698b commit 1e00c81

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/index.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,28 @@ impl Index {
8282
}
8383

8484
pub fn search(&self, query_string: &str) -> Result<ResultsIterator, RedisError> {
85-
/*
86-
* Return an iterator over the results of the specified query string.
87-
* @param[out] err: if not-NULL, will be set to the error message, if there is a
88-
* problem parsing the query
89-
* @return an iterator over the results, or NULL if no iterator can be had
90-
* (see err, or no results).
91-
*/
92-
debug!("Querying: '{}'", query_string);
93-
9485
let c_query = CString::new(query_string).unwrap();
95-
let mut err_buf = Vec::<u8>::with_capacity(1024);
86+
let mut err_ptr = ptr::null_mut();
9687

9788
let results_iter = unsafe {
9889
raw::RediSearch_IterateQuery(
9990
self.inner,
10091
c_query.as_ptr(),
10192
query_string.len(),
102-
&mut (err_buf.as_mut_ptr() as *mut c_char),
93+
&mut err_ptr,
10394
)
10495
};
10596

106-
Ok(ResultsIterator::from_raw(results_iter, self, err_buf)?)
97+
if !err_ptr.is_null() {
98+
let err = unsafe { CStr::from_ptr(err_ptr) }.to_str()?.to_owned();
99+
100+
// FIXME: free() the err_ptr value.
101+
// This should be exposed from the RediSearch API. Talk to Meir.
102+
103+
return Err(err.into());
104+
}
105+
106+
Ok(ResultsIterator::from_raw(results_iter, self)?)
107107
}
108108
}
109109

@@ -116,18 +116,7 @@ impl<'idx> ResultsIterator<'idx> {
116116
fn from_raw(
117117
results_iter: *mut RSResultsIterator,
118118
index: &'idx Index,
119-
err_buf: Vec<u8>,
120119
) -> Result<Self, RedisError> {
121-
if results_iter.is_null() {
122-
// Either we encountered an error, or there are no results.
123-
let err = String::from_utf8(err_buf)?;
124-
125-
// TODO: This is quite ugly. There should be a nicer way to know if there was an error.
126-
if err.len() > 0 {
127-
return Err(err.into());
128-
}
129-
}
130-
131120
Ok(Self {
132121
inner: results_iter,
133122
index,
@@ -167,6 +156,10 @@ impl Iterator for ResultsIterator<'_> {
167156

168157
impl Drop for ResultsIterator<'_> {
169158
fn drop(&mut self) {
159+
if self.inner.is_null() {
160+
return;
161+
}
162+
170163
debug!("Freeing results iterator");
171164
unsafe {
172165
raw::RediSearch_ResultsIteratorFree(self.inner);

0 commit comments

Comments
 (0)