Added error codes to store.

This commit is contained in:
Jeff Baskin 2023-03-15 07:38:39 -04:00
parent a3d8feb37a
commit e125d79a6c
2 changed files with 35 additions and 26 deletions

View File

@ -312,9 +312,9 @@ mod datatype_file {
let mut feed = data.iter();
match DataType::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have raised an error")),
Err(err) => {
assert_eq!(err.to_string(), "file corruption");
Ok(())
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
}
}
}

View File

@ -1,4 +1,4 @@
use super::{DBError, FileData, SessionData};
use super::{DBError, ErrorCode, FileData, SessionData};
use std::{collections::HashMap, slice, str};
#[derive(Clone)]
@ -46,7 +46,7 @@ impl FileData<Self> for Store {
Some(a) => letter = a.clone(),
None => {
if !name.is_empty() {
return Err(DBError::new(err_msg));
return Err(DBError::from_code(ErrorCode::CorruptFile));
}
break;
}
@ -55,16 +55,16 @@ impl FileData<Self> for Store {
if get_id {
let name_holder = match str::from_utf8(&name) {
Ok(item) => item,
Err(_) => return Err(DBError::new(err_msg)),
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
let id_holder = match str::from_utf8(&id) {
Ok(item) => item,
Err(_) => return Err(DBError::new(err_msg)),
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
match output.add("database", name_holder, id_holder) {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new(err_msg);
let mut error = DBError::from_code(ErrorCode::CorruptFile);
error.add_source(err);
return Err(error);
}
@ -168,29 +168,35 @@ mod file_data {
}
#[test]
fn from_bytes_incomplete_name() {
fn from_bytes_incomplete_name() -> Result<(), DBError> {
let data = "notName".as_bytes();
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => assert!(false, "This should have failed."),
Err(err) => assert_eq!(err.to_string(), "file corruption"),
Ok(_) => Err(DBError::new("should have produced an errpr")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[test]
fn from_bytes_incomplete_id() {
fn from_bytes_incomplete_id() -> Result<(), DBError> {
let mut data = "proper".as_bytes().to_vec();
data.push(0);
data.append(&mut "nope".as_bytes().to_vec());
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => assert!(false, "This should have failed."),
Err(err) => assert_eq!(err.to_string(), "file corruption"),
Ok(_) => Err(DBError::new("should have produced an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
}
}
}
#[test]
fn from_bytes_handles_error() {
fn from_bytes_handles_error() -> Result<(), DBError> {
let name = "duplicate";
let mut data = name.as_bytes().to_vec();
data.push(0);
@ -202,17 +208,20 @@ mod file_data {
data.push(0);
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => assert!(false, "This should have failed."),
Err(err) => {
assert_eq!(err.to_string(), "file corruption");
assert!(
err.source().is_some(),
"Should state file corruption cause."
);
assert_eq!(
err.source().unwrap().to_string(),
format!("database {} already exists", name)
);
Ok(_) => Err(DBError::new("should have returned an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => {
assert!(
err.source().is_some(),
"Should state file corruption cause."
);
assert_eq!(
err.source().unwrap().to_string(),
format!("database {} already exists", name)
);
Ok(())
},
_ => Err(DBError::new("incorrect error code")),
}
}
}