Skip to content

Commit

Permalink
Small changes in errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GPeaky committed Oct 19, 2024
1 parent 7b14fc8 commit 77333dc
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion crates/api/src/handlers/auth/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub(crate) async fn forgot_password(

if let Some(last_update) = user.updated_at {
if Utc::now().signed_duration_since(last_update) > Duration::hours(1) {
return Err(UserError::UpdateLimitExceeded)?;
return Err(CommonError::UpdateLimit)?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/handlers/championships/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub async fn stream_telemetry_session(
match relation {
Some(relation) => {
if relation.role != ChampionshipRole::Engineer || relation.team_id.is_none() {
Err(ChampionshipError::NotEngineer)?
Err(ChampionshipError::NotAnEngineer)?
}

let team_id = relation.team_id.unwrap() as u8;
Expand Down
6 changes: 3 additions & 3 deletions crates/api/src/handlers/user/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn remove_user(
};

if path.0 == user_id {
Err(UserError::AutoDelete)?
Err(UserError::SelfDelete)?
}

state.user_svc.delete(path.0).await?;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub async fn deactivate_user_account(
let user_id = req.user_id()?;

if path.0 == user_id {
Err(UserError::AutoDelete)?
Err(UserError::SelfDelete)?
}

state.user_svc.admin_deactivate(path.0).await?;
Expand Down Expand Up @@ -97,7 +97,7 @@ pub async fn reactivate_user_account(
let user_id = req.user_id()?;

if path.0 == user_id {
Err(UserError::AutoDelete)?
Err(UserError::SelfDelete)?
}

state.user_svc.admin_activate(path.0).await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/middlewares/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
if now > entry.1 {
*entry = (0, now + LOGIN_RATE_LIMIT_DUR);
} else if entry.0 > LOGIN_RATE_LIMIT {
return Err(CommonError::LoginRateLimited)?;
return Err(CommonError::RateLimited)?;
}

entry.0 += 1;
Expand Down
1 change: 1 addition & 0 deletions crates/discord/src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct RaceData {
impl Deref for DiscordClient {
type Target = DiscordClientInner;

#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
Expand Down
9 changes: 3 additions & 6 deletions crates/error/src/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ pub enum ChampionshipError {
LimitReached,
NotOwner,
CannotRemoveOwner,
IntervalNotReached,
NoPortsAvailable,
InvalidTeamId,
NotEngineer,
NotAnEngineer,
}

impl std::error::Error for ChampionshipError {}
Expand All @@ -36,10 +35,9 @@ impl ChampionshipError {
ChampionshipError::LimitReached => StatusCode::BAD_REQUEST,
ChampionshipError::NotOwner => StatusCode::UNAUTHORIZED,
ChampionshipError::CannotRemoveOwner => StatusCode::BAD_REQUEST,
ChampionshipError::IntervalNotReached => StatusCode::BAD_REQUEST,
ChampionshipError::NoPortsAvailable => StatusCode::SERVICE_UNAVAILABLE,
ChampionshipError::InvalidTeamId => StatusCode::BAD_REQUEST,
ChampionshipError::NotEngineer => StatusCode::UNAUTHORIZED,
ChampionshipError::NotAnEngineer => StatusCode::UNAUTHORIZED,
}
}

Expand All @@ -50,10 +48,9 @@ impl ChampionshipError {
ChampionshipError::LimitReached => "Championship limit reached",
ChampionshipError::NotOwner => "Not Owner of Championship",
ChampionshipError::CannotRemoveOwner => "Cannot remove owner of Championship",
ChampionshipError::IntervalNotReached => "Interval update time not reached",
ChampionshipError::NoPortsAvailable => "No ports available",
ChampionshipError::InvalidTeamId => "Invalid Team Id",
ChampionshipError::NotEngineer => "Not an engineer",
ChampionshipError::NotAnEngineer => "Not an engineer",
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions crates/error/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub enum CommonError {
InternalServerError,
HashingFailed,
NotValidUpdate,
LoginRateLimited,
RateLimited,
UpdateLimit,
}

impl CommonError {
Expand All @@ -21,17 +22,19 @@ impl CommonError {
CommonError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
CommonError::HashingFailed => StatusCode::INTERNAL_SERVER_ERROR,
CommonError::NotValidUpdate => StatusCode::BAD_REQUEST,
CommonError::LoginRateLimited => StatusCode::TOO_MANY_REQUESTS,
CommonError::RateLimited => StatusCode::TOO_MANY_REQUESTS,
CommonError::UpdateLimit => StatusCode::TOO_MANY_REQUESTS,
}
}

pub const fn error_message(&self) -> &'static str {
match self {
CommonError::ValidationFailed => "Data validation failed",
CommonError::InternalServerError => "Internal Server Error",
CommonError::InternalServerError => "Internal server error",
CommonError::HashingFailed => "Hashing Failed",
CommonError::NotValidUpdate => "Not valid Update",
CommonError::LoginRateLimited => "Rate limited",
CommonError::RateLimited => "Rate limited",
CommonError::UpdateLimit => "Update limit exceeded",
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/error/src/f1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::AppError;

#[derive(Debug)]
pub enum F1ServiceError {
AlreadyExists,
AlreadyStarted,
NotActive,
InvalidPacketType,
CastingError,
Expand All @@ -15,23 +15,23 @@ pub enum F1ServiceError {
impl F1ServiceError {
pub const fn status_code(&self) -> StatusCode {
match self {
F1ServiceError::AlreadyExists => StatusCode::CONFLICT,
F1ServiceError::NotActive => StatusCode::INTERNAL_SERVER_ERROR,
F1ServiceError::AlreadyStarted => StatusCode::CONFLICT,
F1ServiceError::NotActive => StatusCode::SERVICE_UNAVAILABLE,
F1ServiceError::InvalidPacketType => StatusCode::INTERNAL_SERVER_ERROR,
F1ServiceError::Shutdown => StatusCode::INTERNAL_SERVER_ERROR,
F1ServiceError::CastingError => StatusCode::INTERNAL_SERVER_ERROR,
F1ServiceError::UnsupportedFormat => StatusCode::INTERNAL_SERVER_ERROR,
F1ServiceError::UnsupportedFormat => StatusCode::BAD_REQUEST,
}
}

pub const fn error_message(&self) -> &'static str {
match self {
F1ServiceError::AlreadyExists => "Already Exists",
F1ServiceError::AlreadyStarted => "Service already started",
F1ServiceError::NotActive => "Service not active",
F1ServiceError::InvalidPacketType => "Invalid packet type",
F1ServiceError::Shutdown => "Error shutting down service",
F1ServiceError::CastingError => "Error casting data",
F1ServiceError::UnsupportedFormat => "Unsupported Format",
F1ServiceError::UnsupportedFormat => "Unsupported udp format",
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions crates/error/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub enum TokenError {
InvalidToken,
MissingToken,
ExpiredToken,
TokenCreationError,
InvalidTokenPurpose,
}

impl std::error::Error for TokenError {}
Expand All @@ -31,8 +29,6 @@ impl TokenError {
TokenError::InvalidToken => StatusCode::UNAUTHORIZED,
TokenError::MissingToken => StatusCode::BAD_REQUEST,
TokenError::ExpiredToken => StatusCode::BAD_REQUEST,
TokenError::TokenCreationError => StatusCode::INTERNAL_SERVER_ERROR,
TokenError::InvalidTokenPurpose => StatusCode::BAD_REQUEST,
}
}

Expand All @@ -41,8 +37,6 @@ impl TokenError {
TokenError::InvalidToken => "Invalid token",
TokenError::MissingToken => "Missing token",
TokenError::ExpiredToken => "Expired token",
TokenError::TokenCreationError => "Token Validation Error",
TokenError::InvalidTokenPurpose => "Invalid token type",
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions crates/error/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ pub enum UserError {
NotVerified,
DiscordAuth,
Unauthorized,
AutoDelete,
SelfDelete,
AlreadyActive,
AlreadyInactive,
WrongProvider,
InvalidUpdate,
UpdateLimitExceeded,
}

impl UserError {
Expand All @@ -33,12 +32,11 @@ impl UserError {
UserError::NotVerified => StatusCode::UNAUTHORIZED,
UserError::DiscordAuth => StatusCode::BAD_REQUEST,
UserError::Unauthorized => StatusCode::UNAUTHORIZED,
UserError::AutoDelete => StatusCode::BAD_REQUEST,
UserError::SelfDelete => StatusCode::BAD_REQUEST,
UserError::AlreadyActive => StatusCode::BAD_REQUEST,
UserError::AlreadyInactive => StatusCode::BAD_REQUEST,
UserError::WrongProvider => StatusCode::BAD_REQUEST,
UserError::InvalidUpdate => StatusCode::BAD_REQUEST,
UserError::UpdateLimitExceeded => StatusCode::UNAUTHORIZED,
}
}

Expand All @@ -50,12 +48,11 @@ impl UserError {
UserError::NotVerified => "Not verified user",
UserError::DiscordAuth => "Use discord auth",
UserError::Unauthorized => "Unauthorized user",
UserError::AutoDelete => "Cannot Delete Yourself",
UserError::SelfDelete => "Cannot Delete Yourself",
UserError::AlreadyActive => "User Already Active",
UserError::AlreadyInactive => "User is not active",
UserError::WrongProvider => "Using wrong provider",
UserError::InvalidUpdate => "Invalid Update",
UserError::UpdateLimitExceeded => "Update Limit Exceeded",
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/intelli-core/src/services/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ impl ChampionshipServiceOperations for ChampionshipService {

if let Some(last_update) = championship.updated_at {
if Utc::now().signed_duration_since(last_update) <= Duration::days(7) {
Err(ChampionshipError::IntervalNotReached)?
Err(CommonError::UpdateLimit)?
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/intelli-core/src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::info;

use db::{Database, EntityCache};
use entities::{Provider, SharedUser};
use error::{AppResult, UserError};
use error::{AppResult, CommonError, UserError};
use id_generator::IdsGenerator;
use structs::{UserRegistrationData, UserUpdateData};
use utils::slice_iter;
Expand Down Expand Up @@ -285,7 +285,7 @@ impl UserService {

if let Some(last_update) = user.updated_at {
if Utc::now().signed_duration_since(last_update) > Duration::minutes(15) {
return Err(UserError::UpdateLimitExceeded)?;
return Err(CommonError::UpdateLimit)?;
}
}

Expand Down Expand Up @@ -363,7 +363,7 @@ impl UserServiceOperations for UserService {
async fn update(&self, user: SharedUser, form: &UserUpdateData) -> AppResult<()> {
if let Some(last_update) = user.updated_at {
if Utc::now().signed_duration_since(last_update) <= Duration::days(7) {
return Err(UserError::UpdateLimitExceeded)?;
return Err(CommonError::UpdateLimit)?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/telemetry/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl F1ServiceHandler {
/// Starts a new F1 service for the given championship.
pub async fn start(&self, port: i32, championship_id: i32) -> AppResult<()> {
if self.service(&championship_id) {
return Err(F1ServiceError::AlreadyExists.into());
return Err(F1ServiceError::AlreadyStarted)?;
}

let (otx, orx) = oneshot::channel::<()>();
Expand Down

0 comments on commit 77333dc

Please sign in to comment.