Added reply with error.
This commit is contained in:
36
src/queue.rs
36
src/queue.rs
@@ -1,4 +1,4 @@
|
||||
use crate::field::Field;
|
||||
use crate::{field::Field, ErrorType};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{mpsc::Sender, Arc, RwLock},
|
||||
@@ -48,6 +48,12 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reply_with_error(&self, error: ErrorType) -> Self {
|
||||
let mut reply = self.reply(MsgType::Error);
|
||||
reply.add_data("error_type", error);
|
||||
reply
|
||||
}
|
||||
|
||||
pub fn get_msg_type(&self) -> &MsgType {
|
||||
&self.msg_type
|
||||
}
|
||||
@@ -181,6 +187,34 @@ mod messages {
|
||||
msg.reset_id();
|
||||
assert_ne!(msg.get_id(), id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_reply() {
|
||||
let msg = Message::new(MsgType::Time);
|
||||
let errors = [
|
||||
ErrorType::DocumentAlreadyExists,
|
||||
ErrorType::DocumentInvalidRequest,
|
||||
];
|
||||
for error in errors.into_iter() {
|
||||
let reply = msg.reply_with_error(error.clone());
|
||||
assert_eq!(reply.get_id(), msg.get_id());
|
||||
assert_eq!(
|
||||
format!("{:?}", reply.get_msg_type()),
|
||||
format!("{:?}", MsgType::Error)
|
||||
);
|
||||
assert_eq!(
|
||||
format!(
|
||||
"{:?}",
|
||||
reply
|
||||
.get_data("error_type")
|
||||
.unwrap()
|
||||
.to_error_type()
|
||||
.unwrap()
|
||||
),
|
||||
format!("{:?}", error)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
Reference in New Issue
Block a user