Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ members = [
"error_registry",
"schemas",
"transport",
"macros"
]
4 changes: 1 addition & 3 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub trait Runnable: Send + Sync {
async fn run(&mut self);
}


pub struct App {
services: Vec<Box<Mutex<dyn Runnable>>>,
}
Expand All @@ -31,8 +30,7 @@ impl App {
}

pub fn init_logger(self) -> Self {
env_logger::Builder::from_env(env_logger::Env::new()
.filter_or("LOGGER_LEVEL", "debug"))
env_logger::Builder::from_env(env_logger::Env::new().filter_or("LOGGER_LEVEL", "debug"))
.init();
self
}
Expand Down
12 changes: 7 additions & 5 deletions app/src/service_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ impl<P: Agent, G: Schema, S: Service<P, G>, N: Transport + Sync + Send> ServiceA

Some(topic) => Ok(topic
.as_str()
.ok_or_else(||BaseError::<Value>::new(
"Unexpected type".to_string(),
GeneratedError::Common(Common::Unknown).into(),
None,
))?
.ok_or_else(|| {
BaseError::<Value>::new(
"Unexpected type".to_string(),
GeneratedError::Common(Common::Unknown).into(),
None,
)
})?
.to_string()),
};
log::debug!("request {:#?} : ", request);
Expand Down
87 changes: 56 additions & 31 deletions error_registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
//! `BaseError` its custom error data structure.
//! `ErrorType` its enum of possible errors
//! what need to be traced for Realis microservices.
use serde::{Deserialize, Serialize};
use std::{
fmt,
fmt::{Debug, Display, Formatter},
};
use backtrace::Backtrace;
use serde_json::Value;
use crate::generated_errors::{Critical, Db};
use crate::{
custom_errors::{CustomErrorType, EnvLoadedError, Nats as CustomNats},
generated_errors::Common,
};
use backtrace::Backtrace;
use generated_errors::GeneratedError;
use crate::generated_errors::{Critical, Db};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
fmt,
fmt::{Debug, Display, Formatter},
};

pub mod custom_errors;
pub mod generated_errors;
Expand All @@ -33,7 +33,10 @@ pub struct BaseError<D: Debug> {

impl<D: Debug> BaseError<D> {
pub fn is_critical(&self) -> bool {
matches!(self.error_type, ErrorType::Generated(GeneratedError::Critical(_)))
matches!(
self.error_type,
ErrorType::Generated(GeneratedError::Critical(_))
)
}
}

Expand Down Expand Up @@ -93,7 +96,7 @@ impl<D: Debug> Display for BaseError<D> {
}
}

impl<D:Debug> From<tokio::sync::oneshot::error::RecvError> for BaseError<D> {
impl<D: Debug> From<tokio::sync::oneshot::error::RecvError> for BaseError<D> {
fn from(error: tokio::sync::oneshot::error::RecvError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -108,7 +111,7 @@ impl<D:Debug> From<tokio::sync::oneshot::error::RecvError> for BaseError<D> {
}
}

impl<D:Debug> From<tokio::time::error::Elapsed> for BaseError<D> {
impl<D: Debug> From<tokio::time::error::Elapsed> for BaseError<D> {
fn from(error: tokio::time::error::Elapsed) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -123,7 +126,7 @@ impl<D:Debug> From<tokio::time::error::Elapsed> for BaseError<D> {
}
}

impl<D:Debug> From<ratsio::RatsioError> for BaseError<D> {
impl<D: Debug> From<ratsio::RatsioError> for BaseError<D> {
fn from(error: ratsio::RatsioError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -138,7 +141,7 @@ impl<D:Debug> From<ratsio::RatsioError> for BaseError<D> {
}
}

impl<D:Debug> From<tokio::task::JoinError> for BaseError<D> {
impl<D: Debug> From<tokio::task::JoinError> for BaseError<D> {
fn from(error: tokio::task::JoinError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -153,7 +156,7 @@ impl<D:Debug> From<tokio::task::JoinError> for BaseError<D> {
}
}

impl<D:Debug> From<tokio_postgres::Error> for BaseError<D> {
impl<D: Debug> From<tokio_postgres::Error> for BaseError<D> {
fn from(error: tokio_postgres::Error) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -168,7 +171,7 @@ impl<D:Debug> From<tokio_postgres::Error> for BaseError<D> {
}
}

impl<D:Debug> From<deadpool::managed::PoolError<tokio_postgres::Error>> for BaseError<D> {
impl<D: Debug> From<deadpool::managed::PoolError<tokio_postgres::Error>> for BaseError<D> {
fn from(error: deadpool::managed::PoolError<tokio_postgres::Error>) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -183,7 +186,7 @@ impl<D:Debug> From<deadpool::managed::PoolError<tokio_postgres::Error>> for Base
}
}

impl<D:Debug> From<std::net::AddrParseError> for BaseError<D> {
impl<D: Debug> From<std::net::AddrParseError> for BaseError<D> {
fn from(error: std::net::AddrParseError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -198,7 +201,7 @@ impl<D:Debug> From<std::net::AddrParseError> for BaseError<D> {
}
}

impl<D:Debug> From<std::num::ParseIntError> for BaseError<D> {
impl<D: Debug> From<std::num::ParseIntError> for BaseError<D> {
fn from(error: std::num::ParseIntError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -213,7 +216,7 @@ impl<D:Debug> From<std::num::ParseIntError> for BaseError<D> {
}
}

impl<D:Debug> From<hex::FromHexError> for BaseError<D> {
impl<D: Debug> From<hex::FromHexError> for BaseError<D> {
fn from(error: hex::FromHexError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -228,7 +231,7 @@ impl<D:Debug> From<hex::FromHexError> for BaseError<D> {
}
}

impl<D:Debug> From<openssl::error::ErrorStack> for BaseError<D> {
impl<D: Debug> From<openssl::error::ErrorStack> for BaseError<D> {
fn from(error: openssl::error::ErrorStack) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -243,7 +246,7 @@ impl<D:Debug> From<openssl::error::ErrorStack> for BaseError<D> {
}
}

impl<D:Debug> From<dotenv::Error> for BaseError<D> {
impl<D: Debug> From<dotenv::Error> for BaseError<D> {
fn from(error: dotenv::Error) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -258,7 +261,7 @@ impl<D:Debug> From<dotenv::Error> for BaseError<D> {
}
}

impl<D:Debug> From<sqlx::error::Error> for BaseError<D> {
impl<D: Debug> From<sqlx::error::Error> for BaseError<D> {
fn from(error: sqlx::error::Error) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -273,7 +276,7 @@ impl<D:Debug> From<sqlx::error::Error> for BaseError<D> {
}
}

impl<D:Debug> From<deadpool_postgres::CreatePoolError> for BaseError<D> {
impl<D: Debug> From<deadpool_postgres::CreatePoolError> for BaseError<D> {
fn from(error: deadpool_postgres::CreatePoolError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
Expand All @@ -288,6 +291,21 @@ impl<D:Debug> From<deadpool_postgres::CreatePoolError> for BaseError<D> {
}
}

impl<D: Debug> From<std::str::ParseBoolError> for BaseError<D> {
fn from(error: std::str::ParseBoolError) -> Self {
let trace = Backtrace::new();
let msg = error.to_string();
let error_type = ErrorType::from(error);
BaseError {
msg,
trace: format!("{:?}", trace),
status: error_type.clone().into(),
error_type,
data: None,
}
}
}

impl<D: Debug> Default for BaseError<D> {
/// Default BaseError.
///
Expand Down Expand Up @@ -418,7 +436,8 @@ impl From<deadpool::managed::PoolError<tokio_postgres::Error>> for ErrorType {
// Custom DB: ConnectionError
ErrorType::Generated(GeneratedError::Critical(Critical::Db))
}
}impl From<tokio_postgres::Error> for ErrorType {
}
impl From<tokio_postgres::Error> for ErrorType {
fn from(_: tokio_postgres::Error) -> Self {
// Custom DB: ConnectionError
ErrorType::Generated(GeneratedError::Critical(Critical::Db))
Expand Down Expand Up @@ -448,7 +467,9 @@ impl From<sqlx::error::Error> for ErrorType {
| sqlx::error::Error::PoolTimedOut
| sqlx::error::Error::Configuration(_)
| sqlx::error::Error::Tls(_)
| sqlx::error::Error::WorkerCrashed => ErrorType::Generated(GeneratedError::Critical(Critical::Db)),
| sqlx::error::Error::WorkerCrashed => {
ErrorType::Generated(GeneratedError::Critical(Critical::Db))
}
// Generated DB: Invalid Transaction
sqlx::error::Error::Database(_)
| sqlx::error::Error::ColumnDecode { .. }
Expand All @@ -457,11 +478,15 @@ impl From<sqlx::error::Error> for ErrorType {
| sqlx::error::Error::ColumnNotFound(_)
| sqlx::error::Error::ColumnIndexOutOfBounds { .. }
| sqlx::error::Error::Decode(_)
| sqlx::error::Error::Migrate(_) => ErrorType::Generated(GeneratedError::Db(Db::InvalidTransaction)),
| sqlx::error::Error::Migrate(_) => {
ErrorType::Generated(GeneratedError::Db(Db::InvalidTransaction))
}
// Generated DB: Not Found
sqlx::error::Error::RowNotFound => ErrorType::Generated(GeneratedError::Db(Db::NotFound)),
sqlx::error::Error::RowNotFound => {
ErrorType::Generated(GeneratedError::Db(Db::NotFound))
}
// Custom: Default
_ => ErrorType::Custom(CustomErrorType::Default)
_ => ErrorType::Custom(CustomErrorType::Default),
}
}
}
Expand All @@ -487,15 +512,15 @@ impl From<std::num::ParseIntError> for ErrorType {
}
}

impl From<std::net::AddrParseError> for ErrorType {
fn from(_: std::net::AddrParseError) -> Self {
impl From<std::str::ParseBoolError> for ErrorType {
fn from(_: std::str::ParseBoolError) -> Self {
// Custom EnvLoadedError: Convert
ErrorType::Custom(CustomErrorType::EnvLoadedError(EnvLoadedError::Convert))
}
}

impl From<std::str::ParseBoolError> for ErrorType {
fn from(_: std::str::ParseBoolError) -> Self {
impl From<std::net::AddrParseError> for ErrorType {
fn from(_: std::net::AddrParseError) -> Self {
// Custom EnvLoadedError: Convert
ErrorType::Custom(CustomErrorType::EnvLoadedError(EnvLoadedError::Convert))
}
Expand Down
1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0.36"
quote = "1.0"
schemas = { path = "../schemas" }
syn = { version = "1.0", features = ["full", "extra-traits"] }
convert_case = "0.5.0"
24 changes: 18 additions & 6 deletions macros/src/env/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@ pub fn impl_env_macros(item: TokenStream) -> TokenStream {
// .map(|attr| symbol::get_env_meta_items(&attr))
// .flatten()
.for_each(|meta| match meta {
Meta(NameValue(m)) if m.path == symbol::RENAME => field_attributes.field_name = EnvNameAttrs::Rename(m.lit),
Meta(Path(path)) if path == symbol::FLATTEN => field_attributes.field_name = EnvNameAttrs::Flatten,
Meta(NameValue(m)) if m.path == symbol::RENAME_ABS => field_attributes.field_name = EnvNameAttrs::RenameAbs(m.lit),
Meta(Path(path)) if path == symbol::DEFAULT => field_attributes.default_type = EnvDefaultAttrs::Default,
Meta(NameValue(m)) if m.path == symbol::DEFAULT_PATH => field_attributes.default_type = EnvDefaultAttrs::DefaultPath(m.lit),
Meta(NameValue(m)) if m.path == symbol::RENAME => {
field_attributes.field_name = EnvNameAttrs::Rename(m.lit)
}
Meta(Path(path)) if path == symbol::FLATTEN => {
field_attributes.field_name = EnvNameAttrs::Flatten
}
Meta(NameValue(m)) if m.path == symbol::RENAME_ABS => {
field_attributes.field_name = EnvNameAttrs::RenameAbs(m.lit)
}
Meta(Path(path)) if path == symbol::DEFAULT => {
field_attributes.default_type = EnvDefaultAttrs::Default
}
Meta(NameValue(m)) if m.path == symbol::DEFAULT_PATH => {
field_attributes.default_type = EnvDefaultAttrs::DefaultPath(m.lit)
}
_ => {}
});

let env_name = field_attributes.field_name.name(&struct_name.to_string(), &field_name.to_string());
let env_name = field_attributes
.field_name
.name(&struct_name.to_string(), &field_name.to_string());
let postfix = field_attributes.default_type.postfix();

match field.ty {
Expand Down
4 changes: 3 additions & 1 deletion macros/src/env/field_attributes/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ impl EnvNameAttrs {
match self {
EnvNameAttrs::Flatten => field_name.to_string(),
EnvNameAttrs::RenameAbs(Str(str)) => str.value().to_string(),
EnvNameAttrs::Rename(Str(str)) => format!("{}_{}", struct_name.to_string(), str.value()),
EnvNameAttrs::Rename(Str(str)) => {
format!("{}_{}", struct_name.to_string(), str.value())
}
_ => format!("{}_{}", struct_name.to_string(), field_name.to_string()),
}
.to_case(Case::UpperSnake)
Expand Down
5 changes: 4 additions & 1 deletion macros/src/env/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ fn spanned_tokens(s: &syn::LitStr) -> parse::Result<TokenStream> {
}

fn respan(stream: TokenStream, span: Span) -> TokenStream {
stream.into_iter().map(|token| respan_token(token, span)).collect()
stream
.into_iter()
.map(|token| respan_token(token, span))
.collect()
}

fn respan_token(mut token: TokenTree, span: Span) -> TokenTree {
Expand Down
14 changes: 11 additions & 3 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ mod env;
mod gettable;
mod gettable_errors;
mod retry;
mod schema;
mod to_json;

use proc_macro::TokenStream;

use crate::schema::schema_macros;
use crate::{
byte_decode::impl_byte_decode_macros, byte_encode::impl_byte_encode_macros, deserialize_errors::impl_deserialize_errors_macros,
env::env::impl_env_macros, gettable::impl_gettable_macros, gettable_errors::impl_gettable_errors_macros, retry::impl_retry_macros,
to_json::impl_to_json_macros,
byte_decode::impl_byte_decode_macros, byte_encode::impl_byte_encode_macros,
deserialize_errors::impl_deserialize_errors_macros, env::env::impl_env_macros,
gettable::impl_gettable_macros, gettable_errors::impl_gettable_errors_macros,
retry::impl_retry_macros, to_json::impl_to_json_macros,
};

/// # Panics
Expand Down Expand Up @@ -136,3 +139,8 @@ pub fn byte_decode_macro_derive(item: TokenStream) -> TokenStream {
pub fn config_macro_derive(item: TokenStream) -> TokenStream {
impl_env_macros(item)
}

#[proc_macro_derive(Schema)]
pub fn impl_schema_macro(item: TokenStream) -> TokenStream {
schema_macros(item)
}
Loading