Skip to content

Commit

Permalink
fix: add errors module to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioDMFerreira committed Sep 22, 2023
1 parent e6225ad commit 2e1a0fb
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions utils/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use serde::Serialize;

const DATABASE_ERROR_CODE: u32 = 1;
const BROKER_ERROR_CODE: u32 = 2;
const HTTP_ERROR_CODE: u32 = 3;

#[derive(Debug, Serialize, Clone, PartialEq)]
pub struct CommonError {
pub message: String,
pub code: u32,
}

impl std::fmt::Display for CommonError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Error: {}, Code: {}", self.message, self.code)
}
}

#[derive(Debug, Clone)]
pub struct DatabaseError {
pub message: String,
}

impl From<DatabaseError> for CommonError {
fn from(val: DatabaseError) -> Self {
Self {
message: val.message,
code: DATABASE_ERROR_CODE,
}
}
}

#[derive(Debug, Clone)]
pub struct BrokerError {
pub message: String,
}

impl From<BrokerError> for CommonError {
fn from(val: BrokerError) -> Self {
CommonError {
message: val.message,
code: BROKER_ERROR_CODE,
}
}
}

#[derive(Debug, Clone)]
pub struct HttpError {
pub message: String,
}

impl From<HttpError> for CommonError {
fn from(val: HttpError) -> Self {
CommonError {
message: val.message,
code: HTTP_ERROR_CODE,
}
}
}

0 comments on commit 2e1a0fb

Please sign in to comment.