From 43778a62fe6ce151e8fb0de78b8396f5b6c2e908 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 16 Jul 2024 15:44:21 +0300 Subject: [PATCH] feat: New API errors, `Querys` and `NotRegisteredUser` Signed-off-by: Awiteb --- crates/oxidetalis/src/errors.rs | 2 ++ crates/oxidetalis/src/routes/errors.rs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/oxidetalis/src/errors.rs b/crates/oxidetalis/src/errors.rs index 68ef86a..f9b5057 100644 --- a/crates/oxidetalis/src/errors.rs +++ b/crates/oxidetalis/src/errors.rs @@ -53,6 +53,7 @@ impl From for ServerError { impl From for WsError { fn from(err: ServerError) -> Self { match err { + ServerError::Api(ApiError::NotRegisteredUser) => WsError::RegistredUserEvent, ServerError::Internal(_) | ServerError::Api(_) => WsError::InternalServerError, ServerError::Ws(err) => err, } @@ -62,6 +63,7 @@ impl From for WsError { impl From for ApiError { fn from(err: ServerError) -> Self { match err { + ServerError::Ws(WsError::RegistredUserEvent) => ApiError::NotRegisteredUser, ServerError::Internal(_) | ServerError::Ws(_) => ApiError::Internal, ServerError::Api(err) => err, } diff --git a/crates/oxidetalis/src/routes/errors.rs b/crates/oxidetalis/src/routes/errors.rs index 29686de..788559b 100644 --- a/crates/oxidetalis/src/routes/errors.rs +++ b/crates/oxidetalis/src/routes/errors.rs @@ -41,6 +41,13 @@ pub enum ApiError { /// (400 Bad Request) #[error("You entered two different public keys")] TwoDifferentKeys, + /// Error in the query parameters (400 Bad Request) + #[error("{0}")] + Querys(String), + /// Non registered user tried to access to registered user only endpoint + /// (403 Forbidden) + #[error("You are not a registered user, please register first")] + NotRegisteredUser, } impl ApiError { @@ -48,8 +55,10 @@ impl ApiError { pub const fn status_code(&self) -> StatusCode { match self { Self::Internal => StatusCode::INTERNAL_SERVER_ERROR, - Self::RegistrationClosed => StatusCode::FORBIDDEN, - Self::AlreadyRegistered | Self::TwoDifferentKeys => StatusCode::BAD_REQUEST, + Self::RegistrationClosed | Self::NotRegisteredUser => StatusCode::FORBIDDEN, + Self::AlreadyRegistered | Self::TwoDifferentKeys | Self::Querys(_) => { + StatusCode::BAD_REQUEST + } } } }