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