2023-03-14 11:32:37 -04:00
|
|
|
use async_std::path::PathBuf;
|
2022-09-27 07:31:59 -04:00
|
|
|
use std::{error::Error, fmt};
|
2022-07-22 20:34:50 -04:00
|
|
|
|
2023-03-14 11:32:37 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ErrorCode {
|
|
|
|
Undefined(String),
|
|
|
|
// Read Write Errors
|
|
|
|
CorruptFile,
|
|
|
|
// Data Type Errors
|
|
|
|
DataTypeIncorrect(String),
|
|
|
|
// Entry Errors
|
|
|
|
EntryExists(PathBuf),
|
|
|
|
EntryWriteFailure(PathBuf),
|
|
|
|
EntryReadFailure(PathBuf),
|
|
|
|
EntryDeleteFailure(PathBuf),
|
|
|
|
// Cache
|
|
|
|
CacheReadWrite,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ErrorCode {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
|
|
|
|
ErrorCode::DataTypeIncorrect(dtype) => write!(f, "data type '{}' is not valid", dtype),
|
|
|
|
ErrorCode::CorruptFile => write!(f, "corrupt file"),
|
|
|
|
ErrorCode::EntryExists(path) => write!(
|
|
|
|
f,
|
|
|
|
"entry '{}' already exists",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
),
|
|
|
|
ErrorCode::EntryWriteFailure(path) => write!(
|
|
|
|
f,
|
|
|
|
"entry '{}' write failure",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
),
|
|
|
|
ErrorCode::EntryReadFailure(path) => write!(
|
|
|
|
f,
|
|
|
|
"entry '{}' read failure",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
),
|
|
|
|
ErrorCode::EntryDeleteFailure(path) => write!(
|
|
|
|
f,
|
|
|
|
"entry '{}' delete failure",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
),
|
|
|
|
ErrorCode::CacheReadWrite => write!(f, "cache read write"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 20:34:50 -04:00
|
|
|
#[derive(Debug)]
|
2022-09-27 07:31:59 -04:00
|
|
|
pub struct DBError {
|
2023-03-14 11:32:37 -04:00
|
|
|
pub code: ErrorCode,
|
2022-09-27 07:31:59 -04:00
|
|
|
src: Option<Box<dyn Error + 'static>>,
|
2022-07-23 21:28:34 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 07:31:59 -04:00
|
|
|
impl DBError {
|
|
|
|
pub fn new<S>(msg: S) -> Self
|
2022-08-06 12:03:47 -04:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2022-07-22 20:34:50 -04:00
|
|
|
Self {
|
2023-03-14 11:32:37 -04:00
|
|
|
code: ErrorCode::Undefined(msg.into()),
|
|
|
|
src: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_code(code: ErrorCode) -> Self {
|
|
|
|
Self {
|
|
|
|
code: code,
|
2022-09-27 07:31:59 -04:00
|
|
|
src: None,
|
2022-07-22 20:34:50 -04:00
|
|
|
}
|
|
|
|
}
|
2022-07-23 21:28:34 -04:00
|
|
|
|
2022-09-27 07:31:59 -04:00
|
|
|
pub fn add_source<E>(&mut self, src: E)
|
2022-07-23 21:50:37 -04:00
|
|
|
where
|
2022-09-27 07:31:59 -04:00
|
|
|
E: Error + 'static,
|
2022-07-23 21:50:37 -04:00
|
|
|
{
|
2022-09-27 07:31:59 -04:00
|
|
|
self.src = Some(Box::new(src));
|
2022-07-23 21:28:34 -04:00
|
|
|
}
|
2022-07-22 20:34:50 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 07:31:59 -04:00
|
|
|
impl Error for DBError {
|
2022-07-23 21:28:34 -04:00
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
2022-09-27 07:31:59 -04:00
|
|
|
match &self.src {
|
2022-07-23 21:28:34 -04:00
|
|
|
Some(err) => Some(err.as_ref()),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:31:59 -04:00
|
|
|
impl fmt::Display for DBError {
|
2022-07-22 20:34:50 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-03-14 11:32:37 -04:00
|
|
|
write!(f, "{}", self.code)
|
2022-07-22 20:34:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-03-14 11:32:37 -04:00
|
|
|
mod errors {
|
2022-08-06 12:03:47 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2022-09-27 07:31:59 -04:00
|
|
|
fn with_str() {
|
|
|
|
let msg = "something happened";
|
|
|
|
let err = DBError::new(msg);
|
2022-08-06 12:03:47 -04:00
|
|
|
assert!(
|
2022-09-27 07:31:59 -04:00
|
|
|
err.to_string() == msg,
|
|
|
|
"Got: {} -- Want: {}",
|
2022-08-06 12:03:47 -04:00
|
|
|
err.to_string(),
|
2022-09-27 07:31:59 -04:00
|
|
|
msg
|
2022-08-06 12:03:47 -04:00
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
err.source().is_none(),
|
2022-09-27 07:31:59 -04:00
|
|
|
"Error should initialize with no source."
|
2022-08-06 12:03:47 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-27 07:31:59 -04:00
|
|
|
fn with_string() {
|
|
|
|
let msg = "it went boom".to_string();
|
|
|
|
let err = DBError::new(msg.clone());
|
2022-08-06 12:03:47 -04:00
|
|
|
assert!(
|
2022-09-27 07:31:59 -04:00
|
|
|
err.to_string() == msg,
|
|
|
|
"Got: {} -- Want: {}",
|
2022-08-06 12:03:47 -04:00
|
|
|
err.to_string(),
|
2022-09-27 07:31:59 -04:00
|
|
|
msg
|
2022-07-22 20:34:50 -04:00
|
|
|
);
|
2022-07-23 21:28:34 -04:00
|
|
|
assert!(
|
|
|
|
err.source().is_none(),
|
2022-09-27 07:31:59 -04:00
|
|
|
"Error should initialize with no source."
|
2022-08-06 12:03:47 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-14 11:32:37 -04:00
|
|
|
#[test]
|
|
|
|
fn using_error_code() {
|
|
|
|
let msg = "utter failure";
|
|
|
|
let code = ErrorCode::Undefined(msg.to_string());
|
|
|
|
let err = DBError::from_code(code);
|
|
|
|
assert_eq!(err.to_string(), msg);
|
|
|
|
assert!(err.source().is_none(), "Should be no source");
|
|
|
|
}
|
|
|
|
|
2022-08-06 12:03:47 -04:00
|
|
|
#[test]
|
|
|
|
fn with_source() {
|
2022-09-27 07:31:59 -04:00
|
|
|
let msg = "but this caused the problem";
|
|
|
|
let mut par = DBError::new("parent error");
|
|
|
|
let src = DBError::new(msg);
|
|
|
|
par.add_source(src);
|
|
|
|
let output = par.source();
|
|
|
|
assert!(output.is_some(), "Should return source.");
|
|
|
|
let source = output.unwrap();
|
|
|
|
assert!(source.to_string() == msg);
|
2022-07-22 20:34:50 -04:00
|
|
|
}
|
|
|
|
}
|
2023-03-14 11:32:37 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod codes {
|
|
|
|
use super::*;
|
|
|
|
use async_std::path::PathBuf;
|
|
|
|
|
|
|
|
const items: [&str; 2] = ["first", "second"];
|
|
|
|
|
|
|
|
fn create_path_buffer() -> Vec<PathBuf> {
|
|
|
|
let mut output = Vec::new();
|
|
|
|
for item in items {
|
|
|
|
let mut path = PathBuf::new();
|
|
|
|
path.push("thepath");
|
|
|
|
path.push(item);
|
|
|
|
output.push(path);
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn undefined_display() {
|
|
|
|
for item in items {
|
|
|
|
let err = ErrorCode::Undefined(item.to_string());
|
|
|
|
assert_eq!(err.to_string(), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn incorrect_data_type() {
|
|
|
|
for item in items {
|
|
|
|
let err = ErrorCode::DataTypeIncorrect(item.to_string());
|
|
|
|
assert_eq!(
|
|
|
|
err.to_string(),
|
|
|
|
format!("data type '{}' is not valid", item)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn corrupt_file() {
|
|
|
|
assert_eq!(ErrorCode::CorruptFile.to_string(), "corrupt file");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn entry_exists() {
|
|
|
|
for path in create_path_buffer() {
|
|
|
|
let err = ErrorCode::EntryExists(path.clone());
|
|
|
|
assert_eq!(
|
|
|
|
err.to_string(),
|
|
|
|
format!(
|
|
|
|
"entry '{}' already exists",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn entry_write_failure() {
|
|
|
|
for path in create_path_buffer() {
|
|
|
|
let err = ErrorCode::EntryWriteFailure(path.clone());
|
|
|
|
assert_eq!(
|
|
|
|
err.to_string(),
|
|
|
|
format!(
|
|
|
|
"entry '{}' write failure",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn entry_read_failure() {
|
|
|
|
for path in create_path_buffer() {
|
|
|
|
let err = ErrorCode::EntryReadFailure(path.clone());
|
|
|
|
assert_eq!(
|
|
|
|
err.to_string(),
|
|
|
|
format!(
|
|
|
|
"entry '{}' read failure",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn entry_delete_failure() {
|
|
|
|
for path in create_path_buffer() {
|
|
|
|
let err = ErrorCode::EntryDeleteFailure(path.clone());
|
|
|
|
assert_eq!(
|
|
|
|
err.to_string(),
|
|
|
|
format!(
|
|
|
|
"entry '{}' delete failure",
|
|
|
|
path.file_name().unwrap().to_str().unwrap()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cache_read_write_failure() {
|
|
|
|
let err = ErrorCode::CacheReadWrite;
|
|
|
|
assert_eq!(err.to_string(), "cache read write");
|
|
|
|
}
|
|
|
|
}
|