From 468d91184016c03e586458638533ecd26e8bf042 Mon Sep 17 00:00:00 2001 From: JesusPerez Date: Wed, 1 Sep 2021 17:15:59 +0100 Subject: [PATCH] chore: add app_errors --- app_errors/.gitignore | 10 +++++ app_errors/Cargo.toml | 17 +++++++++ app_errors/README.md | 4 ++ app_errors/TODO.md | 3 ++ app_errors/src/lib.rs | 88 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 app_errors/.gitignore create mode 100644 app_errors/Cargo.toml create mode 100644 app_errors/README.md create mode 100644 app_errors/TODO.md create mode 100644 app_errors/src/lib.rs diff --git a/app_errors/.gitignore b/app_errors/.gitignore new file mode 100644 index 0000000..c3a29c2 --- /dev/null +++ b/app_errors/.gitignore @@ -0,0 +1,10 @@ +/target +target +Cargo.lock +.cache +.temp +.env +*.log +.DS_Store +logs +tmp diff --git a/app_errors/Cargo.toml b/app_errors/Cargo.toml new file mode 100644 index 0000000..6a251a6 --- /dev/null +++ b/app_errors/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "app_errors" +version = "0.1.0" +authors = ["JesusPerez "] +edition = "2018" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +error-chain = "0.12.4" +failure = "0.1.8" +serde = { version = "1.0", features = ["derive"] } +serde_derive = "1.0.125" + +[dev-dependencies] +serde_derive = "1.0.125" diff --git a/app_errors/README.md b/app_errors/README.md new file mode 100644 index 0000000..4b75b2a --- /dev/null +++ b/app_errors/README.md @@ -0,0 +1,4 @@ +### App Error library + +Application Errors definition + diff --git a/app_errors/TODO.md b/app_errors/TODO.md new file mode 100644 index 0000000..026ba54 --- /dev/null +++ b/app_errors/TODO.md @@ -0,0 +1,3 @@ +# App Errors library + +- [ ] Collect errors and sync diff --git a/app_errors/src/lib.rs b/app_errors/src/lib.rs new file mode 100644 index 0000000..c6a4a04 --- /dev/null +++ b/app_errors/src/lib.rs @@ -0,0 +1,88 @@ +/// Main errors definition +// +// Copyright 2021, Jesús Pérez Lorenzo +// +use failure::Fail; +use std::fmt; +use serde::{Serialize}; + +///`AppError` Aplication Errors definition ans display +/// +#[derive(Debug)] +pub enum AppError { +/// when toke not valid + NoValidToken, + NoValidSession, + SSLModeError, + RunningModeError, + UndefinedCollection, + RecordAlreadyExists, + RecordNotFound, + DatabaseError, + NoDataStorePool, + NoAppEnvLoaded, + NoCertsLoaded, + SqlDeleteError, + HasherError, + MailError, + OperationCanceled, + ErrorInternalServerError(String), +} + +#[allow(clippy::pattern_type_mismatch)] +impl fmt::Display for AppError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + AppError::NoValidToken => write!(f, "No valid token found"), + AppError::NoValidSession => write!(f, "No valid session found"), + AppError::SSLModeError => write!(f, "SSL Mode error"), + AppError::RunningModeError => write!(f, "No valid run mode"), + AppError::UndefinedCollection => write!(f, "Collection undefined"), + AppError::RecordAlreadyExists => write!(f, "This record violates a unique constraint"), + AppError::RecordNotFound => write!(f, "This record does not exist"), + AppError::NoDataStorePool => write!(f, "No data store pool"), + AppError::NoAppEnvLoaded => write!(f, "Application environment not loaded.\nReview APP_CONFIG_PATH and config.toml content "), + AppError::NoCertsLoaded => write!(f, "Certifcations not loaded. Review APP_CONFIG_PATH certs_store_path"), + AppError::SqlDeleteError => write!(f, "Sql Delete error"), + AppError::MailError => write!(f, "Mail error "), + AppError::DatabaseError => write!(f, "Database error "), + AppError::HasherError => write!(f, "Hasher error "), + AppError::OperationCanceled => write!(f, "The running operation was canceled"), + AppError::ErrorInternalServerError(e) => write!(f, "Invalid server error {:?}",e), + } + } +} + + +#[derive(Debug, Serialize)] +struct ErrorResponse { + err: String, +} + + +#[derive(Debug, Fail)] +pub enum AppCertificateError { + #[fail(display = "{} - Reason: {}", 0, 1)] + BadFile(String, String), + #[fail(display = "{} - Reason: {}", 0, 1)] + FileReadError(String, String), +} + +#[derive(Debug, Fail)] +pub enum LoginFailed { + #[fail(display = "{} - Reason: {}", 0, 1)] + MissingPassword(String, String), + + #[fail(display = "{} - Reason: {}", 0, 1)] + InvalidPassword(String, String), + + #[fail(display = "{} - Reason: {}", 0, 1)] + InvalidTokenOwner(String, String), + + #[fail(display = "{} - Reason: {}", 0, 1)] + PasswordHashingFailed(String, String), + + #[fail(display = "{} - Reason: {}", 0, 1)] + PasswordVerificationFailed(String, String), + +}