Added errprs.
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:
51
src/error.rs
Normal file
51
src/error.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ErrorType {
|
||||
TableAddFieldDuplicate(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ErrorType::TableAddFieldDuplicate(data) => write!(f, "field '{}' already exists", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MTTError {
|
||||
err: ErrorType,
|
||||
}
|
||||
|
||||
impl MTTError {
|
||||
fn new(err: ErrorType) -> Self {
|
||||
Self {
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for MTTError {}
|
||||
|
||||
impl fmt::Display for MTTError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod errors {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn get_error() {
|
||||
let err = MTTError::new(ErrorType::TableAddFieldDuplicate("tester".to_string()));
|
||||
assert_eq!(err.to_string(), "field 'tester' already exists");
|
||||
assert!(err.source().is_none());
|
||||
let err = MTTError::new(ErrorType::TableAddFieldDuplicate("other".to_string()));
|
||||
assert_eq!(err.to_string(), "field 'other' already exists");
|
||||
assert!(err.source().is_none());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user