Skip to content

Commit

Permalink
Separate parsing errors from general response errors. (#130)
Browse files Browse the repository at this point in the history
Parsing errors are client-side errors, that are caused by bad output from the server. Response errors are server-side errors, caused by bad input from the user / client.
Parse errors cause the client to be in an unrecoverable state. response errors are OK.
  • Loading branch information
nihohit committed Mar 6, 2024
1 parent f387b19 commit 9d48dce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 6 additions & 6 deletions redis/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ where
})
} else {
Err(RedisError::from((
ErrorKind::ResponseError,
ErrorKind::ParseError,
"parse error",
)))
}
Expand Down Expand Up @@ -293,7 +293,7 @@ where
text: text.to_string(),
})
} else {
Err(RedisError::from((ErrorKind::ResponseError, "parse error")))
Err(RedisError::from((ErrorKind::ParseError, "parse error")))
}
})
};
Expand Down Expand Up @@ -358,7 +358,7 @@ mod aio_support {
.map_range(|range| format!("{range:?}"))
.to_string();
return Err(RedisError::from((
ErrorKind::ResponseError,
ErrorKind::ParseError,
"parse error",
err,
)));
Expand Down Expand Up @@ -417,7 +417,7 @@ mod aio_support {
.map_range(|range| format!("{range:?}"))
.map_position(|pos| pos.translate_position(decoder.buffer()))
.to_string();
RedisError::from((ErrorKind::ResponseError, "parse error", err))
RedisError::from((ErrorKind::ParseError, "parse error", err))
}
}
}),
Expand Down Expand Up @@ -475,7 +475,7 @@ impl Parser {
.map_range(|range| format!("{range:?}"))
.map_position(|pos| pos.translate_position(decoder.buffer()))
.to_string();
RedisError::from((ErrorKind::ResponseError, "parse error", err))
RedisError::from((ErrorKind::ParseError, "parse error", err))
}
}
}),
Expand Down Expand Up @@ -627,7 +627,7 @@ mod tests {
let bytes = b"*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1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\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n*1\r\n";
match parse_redis_value(bytes) {
Ok(_) => panic!("Expected Err"),
Err(e) => assert!(matches!(e.kind(), ErrorKind::ResponseError)),
Err(e) => assert!(matches!(e.kind(), ErrorKind::ParseError)),
}
}
}
6 changes: 5 additions & 1 deletion redis/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub enum NumericBehavior {
pub enum ErrorKind {
/// The server generated an invalid response.
ResponseError,
/// The parser failed to parse the server response.
ParseError,
/// The authentication with the server failed.
AuthenticationFailed,
/// Operation failed because of a type mismatch.
Expand Down Expand Up @@ -716,6 +718,7 @@ impl RedisError {
ErrorKind::Serialize => "serializing",
ErrorKind::RESP3NotSupported => "resp3 is not supported by server",
ErrorKind::ConnectionNotFound => "required connection not found",
ErrorKind::ParseError => "parse error",
}
}

Expand Down Expand Up @@ -864,6 +867,7 @@ impl RedisError {

ErrorKind::BusyLoadingError => RetryMethod::WaitAndRetryOnPrimaryRedirectOnReplica,

ErrorKind::ResponseError => RetryMethod::NoRetry,
ErrorKind::ReadOnly => RetryMethod::NoRetry,
ErrorKind::ExtensionError => RetryMethod::NoRetry,
ErrorKind::ExecAbortError => RetryMethod::NoRetry,
Expand All @@ -878,8 +882,8 @@ impl RedisError {
ErrorKind::Serialize => RetryMethod::NoRetry,
ErrorKind::RESP3NotSupported => RetryMethod::NoRetry,

ErrorKind::ParseError => RetryMethod::Reconnect,
ErrorKind::AuthenticationFailed => RetryMethod::Reconnect,
ErrorKind::ResponseError => RetryMethod::Reconnect,
ErrorKind::ConnectionNotFound => RetryMethod::Reconnect,

ErrorKind::IoError => match &self.repr {
Expand Down

0 comments on commit 9d48dce

Please sign in to comment.