completed error move.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
This commit is contained in:
69
src/error.rs
69
src/error.rs
@ -1,12 +1,11 @@
|
||||
use crate::data::{DBError, id::IDError};
|
||||
use crate::data::{id::IDError, DBError, table::TBLError};
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ErrorType {
|
||||
DBErr(DBError),
|
||||
IDErr(IDError),
|
||||
TableAddFieldDuplicate(String),
|
||||
TableRecordInvalidFieldName(String),
|
||||
TBLErr(TBLError),
|
||||
}
|
||||
|
||||
impl From<DBError> for ErrorType {
|
||||
@ -21,16 +20,18 @@ impl From<IDError> for ErrorType {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TBLError> for ErrorType {
|
||||
fn from(value: TBLError) -> Self {
|
||||
ErrorType::TBLErr(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ErrorType::DBErr(data) => write!(f, "database: {}", data),
|
||||
ErrorType::IDErr(data) => write!(f, "id: {}", data),
|
||||
|
||||
ErrorType::TableAddFieldDuplicate(data) => write!(f, "field '{}' already exists", data),
|
||||
ErrorType::TableRecordInvalidFieldName(data) => {
|
||||
write!(f, "invalid field name '{}'", data)
|
||||
}
|
||||
ErrorType::TBLErr(data) => write!(f, "table: {}", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -52,6 +53,13 @@ mod errortypes {
|
||||
let result = ErrorType::from(err.clone());
|
||||
assert_eq!(result.to_string(), format!("id: {}", err));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_error() {
|
||||
let err = TBLError::InvalidField("bad".to_string());
|
||||
let result = ErrorType::from(err.clone());
|
||||
assert_eq!(result.to_string(), format!("table: {}", err));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -83,14 +91,25 @@ impl fmt::Display for MTTError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IDError> for MTTError {
|
||||
fn from(value: IDError) -> Self {
|
||||
Self {
|
||||
err: value.into(),
|
||||
}
|
||||
impl From<DBError> for MTTError {
|
||||
fn from(value: DBError) -> Self {
|
||||
Self { err: value.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IDError> for MTTError {
|
||||
fn from(value: IDError) -> Self {
|
||||
Self { err: value.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TBLError> for MTTError {
|
||||
fn from(value: TBLError) -> Self {
|
||||
Self { err: value.into() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod errors {
|
||||
use super::*;
|
||||
@ -112,24 +131,16 @@ mod errors {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_duplicate_table_name() {
|
||||
let data = rand_str();
|
||||
let etype = ErrorType::TableAddFieldDuplicate(data.clone());
|
||||
let result = MTTError::from(etype);
|
||||
match result.get_code() {
|
||||
ErrorType::TableAddFieldDuplicate(txt) => assert_eq!(txt, &data),
|
||||
_ => unreachable!("should have been ErrorType::FieldIDInvalid"),
|
||||
}
|
||||
assert_eq!(
|
||||
result.to_string(),
|
||||
format!("field '{}' already exists", data)
|
||||
);
|
||||
fn from_database_error() {
|
||||
let error = DBError::DuplicateTable(rand_str());
|
||||
let err = MTTError::from(error.clone());
|
||||
assert_eq!(err.to_string(), ErrorType::DBErr(error).to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_type_strings() {
|
||||
let data = rand_str();
|
||||
let etype = ErrorType::TableRecordInvalidFieldName(data.clone());
|
||||
assert_eq!(etype.to_string(), format!("invalid field name '{}'", data));
|
||||
fn from_table_error() {
|
||||
let error = TBLError::InvalidField(rand_str());
|
||||
let err = MTTError::from(error.clone());
|
||||
assert_eq!(err.to_string(), ErrorType::TBLErr(error).to_string());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user