Made MTTError a proper Error structure.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
This commit is contained in:
@@ -8,43 +8,29 @@ use std::{error::Error, fmt};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum MTTError {
|
||||
AdditionMissingField(Name),
|
||||
CannotConvertMessageToRouteID,
|
||||
DocumentAlreadyExists(String),
|
||||
DocumentFieldAlreadyExists(String, Field),
|
||||
DocumentFieldMissing(String),
|
||||
DocumentFieldNotFound(String),
|
||||
DocumentFieldWrongDataType(FieldType, FieldType),
|
||||
DocumentNotFound(String),
|
||||
FieldDuplicate,
|
||||
pub enum ErrorID {
|
||||
DocumentNameAlreadyExists,
|
||||
DocumentNotFound,
|
||||
FieldDataMustBeUnique,
|
||||
FieldInvalidNone,
|
||||
FieldInvalidType,
|
||||
FieldMissingData,
|
||||
InvalidNone,
|
||||
RecordMismatch,
|
||||
NameDuplicate(Name),
|
||||
NameInvalidID(Uuid),
|
||||
NameMissingTranslation(Language),
|
||||
NameNotUniquePerLanguage(Name),
|
||||
NameNotFound(Name),
|
||||
QueryCannotChangeData,
|
||||
RouteRequiresDocumentID,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ErrorID {
|
||||
InvalidDocumentName,
|
||||
FieldNameAlreadyExists,
|
||||
IndexEntryAlreadyExists,
|
||||
InvalidFieldName(Name),
|
||||
NameAlreadyExists,
|
||||
NameLanguageNotUnique,
|
||||
NameNotFound,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct MTTError2 {
|
||||
pub struct MTTError {
|
||||
doc_name: NameType,
|
||||
error_id: ErrorID,
|
||||
}
|
||||
|
||||
impl MTTError2 {
|
||||
pub fn new<NT>(doc_name: NT, error: ErrorID) -> MTTError2
|
||||
impl MTTError {
|
||||
pub fn new<NT>(doc_name: NT, error: ErrorID) -> MTTError
|
||||
where
|
||||
NT: Into<NameType>,
|
||||
{
|
||||
@@ -59,15 +45,15 @@ impl MTTError2 {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for MTTError2 {
|
||||
impl fmt::Display for MTTError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?} error: {:?}", self.doc_name, self.error_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for MTTError2 {}
|
||||
impl Error for MTTError {}
|
||||
|
||||
impl MessageAction for MTTError2 {
|
||||
impl MessageAction for MTTError {
|
||||
fn doc_name(&self) -> &NameType {
|
||||
&self.doc_name
|
||||
}
|
||||
@@ -81,12 +67,12 @@ mod errors {
|
||||
fn can_create_error() {
|
||||
let doc_name = Name::english(Uuid::new_v4().to_string().as_str());
|
||||
let expected_name: NameType = doc_name.clone().into();
|
||||
let error = ErrorID::InvalidDocumentName;
|
||||
let err = MTTError2::new(doc_name.clone(), error.clone());
|
||||
let error = ErrorID::DocumentNameAlreadyExists;
|
||||
let err = MTTError::new(doc_name.clone(), error.clone());
|
||||
assert_eq!(err.doc_name(), &expected_name);
|
||||
let err_id = err.error_id();
|
||||
match err_id {
|
||||
ErrorID::InvalidDocumentName => {}
|
||||
ErrorID::DocumentNameAlreadyExists => {}
|
||||
_ => unreachable!("got {:?}, should have been invalid document name", err_id),
|
||||
}
|
||||
assert_eq!(
|
||||
@@ -101,7 +87,7 @@ mod errors {
|
||||
let expected_name: NameType = doc_name.clone().into();
|
||||
let field_name = Name::english(Uuid::new_v4().to_string().as_str());
|
||||
let error = ErrorID::InvalidFieldName(field_name.clone());
|
||||
let err = MTTError2::new(doc_name.clone(), error.clone());
|
||||
let err = MTTError::new(doc_name.clone(), error.clone());
|
||||
assert_eq!(err.doc_name(), &expected_name);
|
||||
let err_id = err.error_id();
|
||||
match err_id {
|
||||
|
||||
Reference in New Issue
Block a user