Skip to content

Commit

Permalink
refactor: remove unnecessary "Error" in each error type
Browse files Browse the repository at this point in the history
  • Loading branch information
ParzivalEugene committed Aug 10, 2024
1 parent 0eafe46 commit 0f2a9ee
Show file tree
Hide file tree
Showing 41 changed files with 384 additions and 556 deletions.
6 changes: 3 additions & 3 deletions src/dto/session/interface.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::{
data::models::{Config, Device, Server, Session},
enums::errors::internal::InternalError,
enums::errors::internal::Error,
};

pub trait SessionBy {
async fn get_session<'a>(
&self,
pool: &'a deadpool_diesel::postgres::Pool,
) -> Result<(Session, Device, Config, Server), InternalError>;
) -> Result<(Session, Device, Config, Server), Error>;
}

pub async fn get_session<T: SessionBy>(
pool: &deadpool_diesel::postgres::Pool,
by: T,
) -> Result<(Session, Device, Config, Server), InternalError> {
) -> Result<(Session, Device, Config, Server), Error> {
by.get_session(pool).await
}
6 changes: 3 additions & 3 deletions src/dto/session/query/active_session_and_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
schema,
},
dto::session::SessionBy,
enums::errors::internal::{InternalError, SessionError},
enums::errors::internal::{self, Error},
};
use diesel::prelude::*;
use diesel::QueryDsl;
Expand All @@ -20,7 +20,7 @@ impl SessionBy for ActiveSessionAndDevice {
async fn get_session<'a>(
&self,
pool: &'a deadpool_diesel::postgres::Pool,
) -> Result<(Session, Device, Config, Server), InternalError> {
) -> Result<(Session, Device, Config, Server), Error> {
let conn = pool.get().await?;

let device_id = self.device_id;
Expand All @@ -43,7 +43,7 @@ impl SessionBy for ActiveSessionAndDevice {
if result.len() != 1 {
error!("Session not found for device_id: {}", device_id);

return Err(InternalError::SessionError(SessionError::SessionNotFound));
return Err(Error::Session(internal::Session::SessionNotFound));
}

info!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
schema,
},
dto::session::SessionBy,
enums::errors::internal::{InternalError, SessionError},
enums::errors::internal::{self, Error},
};
use diesel::prelude::*;
use diesel::QueryDsl;
Expand All @@ -21,7 +21,7 @@ impl SessionBy for ActiveSessionAndDeviceAndCountry {
async fn get_session<'a>(
&self,
pool: &'a deadpool_diesel::postgres::Pool,
) -> Result<(Session, Device, Config, Server), InternalError> {
) -> Result<(Session, Device, Config, Server), Error> {
let conn = pool.get().await?;

let (device_id, country) = (self.device_id, self.country);
Expand Down Expand Up @@ -49,7 +49,7 @@ impl SessionBy for ActiveSessionAndDeviceAndCountry {
device_id, country
);

return Err(InternalError::SessionError(SessionError::SessionNotFound));
return Err(Error::Session(internal::Session::SessionNotFound));
}

info!(
Expand Down
92 changes: 92 additions & 0 deletions src/enums/errors/external/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use axum::{
http::StatusCode,
response::{self, IntoResponse},
};
use serde::{Deserialize, Serialize};

use crate::{dto::response::error::Response, enums::errors::internal};

#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum Auth {
WrongToken,
WrongPassword,
WrongEmail,
TokenCreation,
UserNotFound,
UserAlreadyExists,
PasswordIsSame,
OAuthFailed,
OAuthDifferentEmail,
NoClassicAuth,
}

impl std::fmt::Display for Auth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Auth::WrongToken => write!(f, "WrongToken"),
Auth::WrongPassword => write!(f, "WrongPassword"),
Auth::WrongEmail => write!(f, "WrongEmail"),
Auth::TokenCreation => write!(f, "TokenCreation"),
Auth::UserNotFound => write!(f, "UserNotFound"),
Auth::UserAlreadyExists => write!(f, "UserAlreadyExists"),
Auth::PasswordIsSame => write!(f, "PasswordIsSame"),
Auth::OAuthFailed => write!(f, "OAuthFailed"),
Auth::OAuthDifferentEmail => write!(f, "OAuthDifferentEmail"),
Auth::NoClassicAuth => write!(f, "NoClassicAuth"),
}
}
}

impl IntoResponse for Auth {
fn into_response(self) -> response::Response {
let (status, message) = match self {
Auth::WrongToken => (StatusCode::UNAUTHORIZED, "Wrong token"),
Auth::WrongPassword => (StatusCode::UNAUTHORIZED, "Wrong password"),
Auth::WrongEmail => (StatusCode::UNAUTHORIZED, "Wrong email"),
Auth::TokenCreation => (StatusCode::INTERNAL_SERVER_ERROR, "Token creation error"),
Auth::UserNotFound => (StatusCode::NOT_FOUND, "User not found"),
Auth::UserAlreadyExists => (StatusCode::CONFLICT, "User already exists"),
Auth::PasswordIsSame => (StatusCode::CONFLICT, "Password is the same"),
Auth::OAuthFailed => (StatusCode::INTERNAL_SERVER_ERROR, "OAuth failed"),
Auth::OAuthDifferentEmail => (StatusCode::UNAUTHORIZED, "OAuth different email"),
Auth::NoClassicAuth => (
StatusCode::INTERNAL_SERVER_ERROR,
"User has no classic auth",
),
};

Response {
status,
message: message.to_string(),
error: self,
}
.into_response()
}
}

impl From<internal::Auth> for Auth {
fn from(error: internal::Auth) -> Self {
match error {
internal::Auth::WrongPassword => Auth::WrongPassword,
internal::Auth::WrongEmail => Auth::WrongEmail,
internal::Auth::TokenCreation => Auth::TokenCreation,
internal::Auth::UserNotFound => Auth::UserNotFound,
internal::Auth::UserAlreadyExists => Auth::UserAlreadyExists,
internal::Auth::PasswordIsSame => Auth::PasswordIsSame,
internal::Auth::OAuthFailed => Auth::OAuthFailed,
internal::Auth::OAuthDifferentEmail => Auth::OAuthDifferentEmail,
internal::Auth::NoClassicAuth => Auth::NoClassicAuth,
}
}
}

impl From<internal::Reqwest> for Auth {
fn from(error: internal::Reqwest) -> Self {
match error {
internal::Reqwest::Json => Auth::OAuthFailed,
internal::Reqwest::Request => Auth::OAuthFailed,
internal::Reqwest::AccessToken => Auth::OAuthFailed,
}
}
}
97 changes: 0 additions & 97 deletions src/enums/errors/external/auth_error.rs

This file was deleted.

46 changes: 0 additions & 46 deletions src/enums/errors/external/device_error.rs

This file was deleted.

Loading

0 comments on commit 0f2a9ee

Please sign in to comment.