feat: New API errors, Querys and NotRegisteredUser

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-16 15:44:21 +03:00
parent d7a63ea092
commit 43778a62fe
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 13 additions and 2 deletions

View file

@ -53,6 +53,7 @@ impl From<DbErr> for ServerError {
impl From<ServerError> 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<ServerError> for WsError {
impl From<ServerError> 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,
}

View file

@ -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
}
}
}
}