Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract error sorting code to module. #1077

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions glide-core/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use redis::RedisError;
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved

pub enum RequestErrorType {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
Unspecified,
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
ExecAbort,
Timeout,
Disconnect,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClosingError is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would an FFI client return a closing error?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is closing error applicable for UDS clients only?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ATM yes. Only UDS clients have a shared resource that might be reach an unusable state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correction: an FFI wrapper also has a shared resource - the client itself. if the wrapper releases the client, it has reached closing state. but that's pure wrapper-side logic.

}

pub fn error_type(error: &RedisError) -> RequestErrorType {
if error.is_timeout() {
RequestErrorType::Timeout
} else if error.is_unrecoverable_error() {
RequestErrorType::Disconnect
} else if matches!(error.kind(), redis::ErrorKind::ExecAbortError) {
RequestErrorType::ExecAbort
} else {
RequestErrorType::Unspecified
}
}

pub fn error_message(error: &RedisError) -> String {
let error_message = error.to_string();
if matches!(error_type(error), RequestErrorType::Disconnect) {
format!("Received connection error `{error_message}`. Will attempt to reconnect")
} else {
error_message
}
}
1 change: 1 addition & 0 deletions glide-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ mod retry_strategies;
pub mod rotating_buffer;
mod socket_listener;
pub use socket_listener::*;
pub mod errors;
pub mod scripts_container;
33 changes: 13 additions & 20 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use super::rotating_buffer::RotatingBuffer;
use crate::client::Client;
use crate::connection_request::ConnectionRequest;
use crate::errors::{error_message, error_type, RequestErrorType};
use crate::redis_request::{
command, redis_request, Command, RedisRequest, RequestType, Routes, ScriptInvocation,
SlotTypes, Transaction,
Expand Down Expand Up @@ -217,28 +218,20 @@ async fn write_result(
Some(response::response::Value::RequestError(request_error))
}
Err(ClienUsageError::Redis(err)) => {
let error_message = err.to_string();
let error_message = error_message(&err);
log_warn("received error", error_message.as_str());
log_debug("received error", format!("for callback {}", callback_index));
let mut request_error = response::RequestError::default();
if err.is_connection_dropped() {
request_error.type_ = response::RequestErrorType::Disconnect.into();
request_error.message = format!(
"Received connection error `{error_message}`. Will attempt to reconnect"
)
.into();
} else if err.is_timeout() {
request_error.type_ = response::RequestErrorType::Timeout.into();
request_error.message = error_message.into();
} else {
request_error.type_ = match err.kind() {
redis::ErrorKind::ExecAbortError => {
response::RequestErrorType::ExecAbort.into()
}
_ => response::RequestErrorType::Unspecified.into(),
};
request_error.message = error_message.into();
}
let request_error = response::RequestError {
type_: match error_type(&err) {
RequestErrorType::Unspecified => response::RequestErrorType::Unspecified,
RequestErrorType::ExecAbort => response::RequestErrorType::ExecAbort,
RequestErrorType::Timeout => response::RequestErrorType::Timeout,
RequestErrorType::Disconnect => response::RequestErrorType::Disconnect,
}
.into(),
message: error_message.into(),
..Default::default()
};
Some(response::response::Value::RequestError(request_error))
}
};
Expand Down
Loading