use itoa::Buffer;
pub const RESP_ERR_CROSSSLOT: &str = "CROSSSLOT Keys in request do not hash to the same slot";
pub const RESP_ERR_CLUSTERDOWN: &str = "CLUSTERDOWN Hash slot not served";
pub const RESP_ERR_TRYAGAIN: &str = "TRYAGAIN Multiple keys request during rehashing of slot";
pub const RESP_ERR_INVALID_SLOT: &str = "ERR Invalid or out of range slot";
pub const RESP_ERR_GENERIC_SLOT_OUT_OFF_RANGE: &str = "ERR Slot out of range";
pub const RESP_ERR_GENERIC_CONFIG_EPOCH_ASSIGNMENT: &str =
"ERR The user can assign a config epoch only when the node does not know any other node";
pub const RESP_ERR_GENERIC_CONFIG_UPDATE: &str = "ERR Updating the config epoch";
pub const RESP_ERR_GENERIC_CANNOT_FORGET_MYSELF: &str =
"ERR I tried hard but I can't forget myself";
pub const RESP_ERR_GENERIC_CANNOT_FORGET_MY_PRIMARY: &str = "ERR Can't forget my primary";
pub const RESP_ERR_GENERIC_CANNOT_FAILOVER_FROM_NON_MASTER: &str =
"ERR Cannot failover a non-master node";
pub const RESP_ERR_GENERIC_UNKNOWN_ENDPOINT: &str = "ERR Unknown endpoint";
pub const RESP_ERR_GENERIC_SLOT_STATE: &str = "ERR Invalid slot state";
pub const RESP_ERR_MULTI_LOG_DISABLED: &str = "ERR Multi-log disabled";
pub const RESP_ERR_IOERR: &str = "IOERR Migrate keys failed";
pub const RESP_ERR_GENERIC_VALUE_IS_NOT_BOOLEAN: &str = "ERR value is not a boolean.";
#[inline]
pub fn write_redirect_error(
output: &mut Vec<u8>,
kind: &str,
slot: u16,
endpoint: &str,
port: i32,
) {
let mut buf = Buffer::new();
output.push(b'-');
output.extend_from_slice(kind.as_bytes());
output.push(b' ');
output.extend_from_slice(buf.format(slot).as_bytes());
output.push(b' ');
output.extend_from_slice(endpoint.as_bytes());
output.push(b':');
output.extend_from_slice(buf.format(port).as_bytes());
output.extend_from_slice(b"\r\n");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn redirect_error_frames_slot_and_endpoint() {
let mut out = Vec::new();
write_redirect_error(&mut out, "MOVED", 3999, "127.0.0.1", 7001);
assert_eq!(out, b"-MOVED 3999 127.0.0.1:7001\r\n");
let mut out = Vec::new();
write_redirect_error(&mut out, "ASK", 12000, "node2.example.com", 6379);
assert_eq!(out, b"-ASK 12000 node2.example.com:6379\r\n");
}
#[test]
fn cluster_error_constants_match_csharp() {
assert_eq!(
RESP_ERR_CROSSSLOT,
"CROSSSLOT Keys in request do not hash to the same slot"
);
assert_eq!(RESP_ERR_CLUSTERDOWN, "CLUSTERDOWN Hash slot not served");
assert_eq!(
RESP_ERR_TRYAGAIN,
"TRYAGAIN Multiple keys request during rehashing of slot"
);
assert_eq!(
RESP_ERR_GENERIC_CONFIG_UPDATE,
"ERR Updating the config epoch"
);
}
}