Improved query error handling.
This commit is contained in:
parent
41afc07bfa
commit
90ab5809b1
@ -9,15 +9,17 @@ const ENTRY: &str = "EntryPoint";
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ErrorCode {
|
enum ErrorCode {
|
||||||
|
// General
|
||||||
Undefined(String),
|
Undefined(String),
|
||||||
IncorrectDataType(String),
|
// Cache Entry
|
||||||
|
EntryNotFound(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for ErrorCode {
|
impl fmt::Display for ErrorCode {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
|
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
|
||||||
ErrorCode::IncorrectDataType(item) => write!(f, "data type '{}' does not exist", item),
|
ErrorCode::EntryNotFound(id) => write!(f, "entry '{}' was not found", id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,13 +38,10 @@ mod errorcodes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn incorrect_data_type() {
|
fn bad_entry() {
|
||||||
for item in ITEMS {
|
for item in ITEMS {
|
||||||
let err = ErrorCode::IncorrectDataType(item.to_string());
|
let err = ErrorCode::EntryNotFound(item.to_string());
|
||||||
assert_eq!(
|
assert_eq!(err.to_string(), format!("entry '{}' was not found", item));
|
||||||
err.to_string(),
|
|
||||||
format!("data type '{}' does not exist", item)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,12 +106,12 @@ mod errors {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_from_different_code() {
|
fn create_missing_entry() {
|
||||||
let code = ErrorCode::IncorrectDataType("nuts".to_string());
|
let code = ErrorCode::EntryNotFound("an_id".to_string());
|
||||||
let err = MTTError::from_code(code);
|
let err = MTTError::from_code(code);
|
||||||
match err.code {
|
match err.code {
|
||||||
ErrorCode::IncorrectDataType(_) => (),
|
ErrorCode::EntryNotFound(_) => (),
|
||||||
_ => assert!(false, "{:?} is not incorrect data type", err.code),
|
_ => assert!(false, "{:?} is not undefined", err.code),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -274,7 +273,9 @@ impl Cache {
|
|||||||
if id == ENTRY {
|
if id == ENTRY {
|
||||||
output.insert(ENTRY.to_string(), DataType::new("store"));
|
output.insert(ENTRY.to_string(), DataType::new("store"));
|
||||||
} else {
|
} else {
|
||||||
return Err(MTTError::new("fred"));
|
return Err(MTTError::from_code(ErrorCode::EntryNotFound(
|
||||||
|
id.to_string(),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(output)
|
Ok(output)
|
||||||
@ -287,17 +288,13 @@ impl Cache {
|
|||||||
async fn start(&self, listener: Receiver<ToCache>) {
|
async fn start(&self, listener: Receiver<ToCache>) {
|
||||||
loop {
|
loop {
|
||||||
match listener.recv().await.unwrap() {
|
match listener.recv().await.unwrap() {
|
||||||
ToCache::Query(qry) => {
|
ToCache::Query(qry) => match self.query(&qry.ids).await {
|
||||||
match self.query(&qry.ids).await {
|
Ok(data) => qry.reply.send(FromCache::Data(data)).await.unwrap(),
|
||||||
Ok(data) => qry.reply.send(FromCache::Data(data)).await.unwrap(),
|
Err(error) => qry.reply.send(FromCache::Error(error)).await.unwrap(),
|
||||||
Err(error) => qry.reply.send(FromCache::Error(error)).await.unwrap(),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
ToCache::Commit(commit) => {
|
ToCache::Commit(commit) => match self.commit().await {
|
||||||
match self.commit().await {
|
Ok(_) => commit.reply.send(FromCache::Ok).await.unwrap(),
|
||||||
Ok(_) => commit.reply.send(FromCache::Ok).await.unwrap(),
|
Err(error) => commit.reply.send(FromCache::Error(error)).await.unwrap(),
|
||||||
Err(error) => commit.reply.send(FromCache::Error(error)).await.unwrap(),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -376,9 +373,29 @@ mod caches {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn add_database_commit() {
|
async fn get_store() {
|
||||||
//let dir = tempdir().unwrap();
|
let dir = tempdir().unwrap();
|
||||||
//let s_cache = start_cache(dir.path()).await;
|
let cache = Cache::new(dir.path()).await.unwrap();
|
||||||
|
let output = cache.query(&[ENTRY.to_string()].to_vec()).await.unwrap();
|
||||||
|
let result = output.get(ENTRY).unwrap();
|
||||||
|
match result {
|
||||||
|
DataType::DBMap(_) => (),
|
||||||
|
_ => assert!(false, "{:?} should have been an Ok.", result),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn bad_get() {
|
||||||
|
let dir = tempdir().unwrap();
|
||||||
|
let cache = Cache::new(dir.path()).await.unwrap();
|
||||||
|
let bad_id = "really_bad_id";
|
||||||
|
match cache.query(&[bad_id.to_string()].to_vec()).await {
|
||||||
|
Ok(_) => assert!(false, "Should have produced an error."),
|
||||||
|
Err(err) => match err.code {
|
||||||
|
ErrorCode::EntryNotFound(id) => assert_eq!(id, bad_id),
|
||||||
|
_ => assert!(false, "{:?} should have been EntryNotFound.", err.code),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user