Skip to content

Commit

Permalink
Separate parsing errors from general response errors.
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 8, 2024
1 parent 069a952 commit 38e4179
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions redis/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,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 @@ -225,7 +225,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 @@ -283,7 +283,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 @@ -373,7 +373,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\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*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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)),
}
}
}
8 changes: 6 additions & 2 deletions redis/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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 @@ -661,6 +663,7 @@ impl RedisError {
ErrorKind::ClusterConnectionNotFound => "connection to node in cluster not found",
#[cfg(feature = "json")]
ErrorKind::Serialize => "serializing",
ErrorKind::ParseError => "parse error",
}
}

Expand Down Expand Up @@ -807,6 +810,7 @@ impl RedisError {
ErrorKind::MasterNameNotFoundBySentinel => RetryMethod::WaitAndRetry,
ErrorKind::NoValidReplicasFoundBySentinel => RetryMethod::WaitAndRetry,

ErrorKind::ResponseError => RetryMethod::NoRetry,
ErrorKind::ReadOnly => RetryMethod::NoRetry,
ErrorKind::ExtensionError => RetryMethod::NoRetry,
ErrorKind::ExecAbortError => RetryMethod::NoRetry,
Expand All @@ -820,9 +824,9 @@ impl RedisError {
#[cfg(feature = "json")]
ErrorKind::Serialize => RetryMethod::NoRetry,

ErrorKind::ParseError => RetryMethod::Reconnect,
ErrorKind::AuthenticationFailed => RetryMethod::Reconnect,
ErrorKind::ResponseError => RetryMethod::Reconnect,
ErrorKind::ClusterConnectionNotFound => todo!(),
ErrorKind::ClusterConnectionNotFound => RetryMethod::Reconnect,

ErrorKind::IoError => match &self.repr {
ErrorRepr::IoError(err) => match err.kind() {
Expand Down

0 comments on commit 38e4179

Please sign in to comment.