Skip to content

Commit

Permalink
feat(error): Provide more information about the occuret error
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Jun 14, 2022
1 parent 778b195 commit a9f9a6b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 75 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bitflags = "1.3.2"
dashmap = "5.3.4"
include_dir = "0.7.2"
regex = "1.5.6"
thiserror = "1.0.31"
quick-error = "2.0.1"

# Docs
utoipa = "1"
8 changes: 3 additions & 5 deletions src/extractors/validate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::error::{Error, ValidationError};
use crate::utils::error::Error;
use axum::{
body::HttpBody,
extract::{FromRequest, Json, RequestParts},
Expand Down Expand Up @@ -26,13 +26,11 @@ where
if let Ok(data) = data {
let data: Json<T> = data;

data.validate().map_err(|error| Error::ValidationFailed {
error: ValidationError(error),
})?;
data.validate().map_err(|_| Error::InvalidBody)?;

Ok(Self(data.0))
} else {
Err(Error::ParseFailed)
Err(Error::InvalidBody)
}
}
}
3 changes: 1 addition & 2 deletions src/routes/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
};
use crate::middlewares::ratelimit::RateLimitInfo;
use crate::structures::*;
use crate::utils::{Badges, Error, Permissions, ValidationError};
use crate::utils::{Badges, Error, Permissions};
use axum::{extract::Json, routing::get, Router};
use utoipa::{
openapi::security::{ApiKey, ApiKeyValue, SecurityScheme},
Expand Down Expand Up @@ -100,7 +100,6 @@ use utoipa::{
CreateRoleOptions,
UpdateRoleOptions,
UpdateMemberOptions,
ValidationError
),
modifiers(&SecurityAddon),
tags(
Expand Down
112 changes: 46 additions & 66 deletions src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,49 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Json, Response},
};
use quick_error::quick_error;
use serde::Serialize;
use validator::ValidationErrors;
use std::fmt::Debug;

#[derive(Debug, Serialize)]
pub struct ValidationError(pub ValidationErrors);
quick_error! {
#[derive(Debug, Serialize, utoipa::Component)]
pub enum Error {
RateLimited(info: RateLimitInfo) {
from(RateLimitInfo)
display("Executed the rate limit. Please retry after {}s", info.retry_after)
}
InvalidBody { display("You have provided a bad json schema") }
MissingHeader { display("Missing header") }
AccountVerificationRequired { display("You need to verify your account in order to perform this action") }
InvalidToken { display("Unauthorized. Provide a valid token and try again") }
EmailAlreadyInUse { display("This email already in use") }
MissingPermissions { display("You lack permissions to perform that action") }
EmptyMessage { display("Cannot send an empty message") }
RequireInviteCode { display("You must have an invite code to perform this action") }
InviteAlreadyTaken { display("This invite already used") }
FailedCaptcha { display("Respect the captcha, Respect you") }
MissingAccess { display("You missing access to perform this action ") }

#[derive(thiserror::Error, Debug, Serialize, utoipa::Component)]
#[serde(tag = "type")]
pub enum Error {
#[error("Invalid body")]
ValidationFailed { error: ValidationError },
#[error("You have executed the rate limit")]
RateLimited(RateLimitInfo),
#[error("Invalid JSON")]
ParseFailed,
#[error("You need to verify your account in order to perform this action")]
AccountVerificationRequired,
#[error("Unauthorized. Provide a valid token and try again")]
InvalidToken,
#[error("Missing header")]
MissingHeader,
#[error("This email already in use")]
EmailAlreadyInUse,
#[error("This username taken by someone else")]
UsernameTaken,
#[error("Captcha don't love you")]
FailedCaptcha,
#[error("You lack permissions to perform that action")]
MissingPermissions,
#[error("You missing access to do the following action")]
MissingAccess,
#[error("Cannot send an empty message")]
EmptyMessage,
#[error("You must habe an invite to register")]
RequireInviteCode,
#[error("This invite already taken")]
InviteAlreadyTaken,
UnknownAccount
UnknownBot
UnknownChannel
UnknownInvite
UnknownUser
UnknownMessage
UnknownServer
UnknownSession
UnknownRole
UnknownMember
Unknown { display("Unknown error has occurred") }

// Unknown
#[error("Unknwon account")]
UnknownAccount,
#[error("Unknwon session")]
UnknownSession,
#[error("Unknwon user")]
UnknownUser,
#[error("Unknwon message")]
UnknownMessage,
#[error("Unknwon server")]
UnknownServer,
#[error("Unknwon member")]
UnknownMember,
#[error("Unknwon role")]
UnknownRole,
#[error("Unknwon bot")]
UnknownBot,
#[error("Unknwon channel")]
UnknownChannel,
#[error("Unknwon invite")]
UnknownInvite,
#[error("Unknwon error")]
Unknown,
MaximumFriends { display("Maximum number of friends reached") }
MaximumServers { display("Maximum number of servers reached") }
MaximumGroups { display("Maximum number of groups reached") }
MaximumRoles { display("Maximum number of server roles reached") }
MaximumChannels { display("Maximum number of channels reached") }
MaximumGroupMembers { display("Maximum number of group members reached") }
MaximumBots { display("Maximum number of bots reached") }
}
}

pub type Result<T, E = Error> = std::result::Result<T, E>;
Expand All @@ -75,16 +57,14 @@ impl IntoResponse for Error {
Error::InvalidToken => StatusCode::UNAUTHORIZED,
_ => StatusCode::BAD_REQUEST,
};
(status, Json(serde_json::json!(self))).into_response()
}
}

use utoipa::openapi::{schema::Component, ComponentType, PropertyBuilder};
let mut body = serde_json::json!({ "type": self });
let msg = self.to_string();

if msg.contains(' ') {
body["message"] = serde_json::json!(msg);
}

impl utoipa::Component for ValidationError {
fn component() -> Component {
PropertyBuilder::new()
.component_type(ComponentType::Object)
.into()
(status, Json(body)).into_response()
}
}

0 comments on commit a9f9a6b

Please sign in to comment.