From 6a739e2dade26431d768fdfa4e32dc9a533da1ee Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 22 Mar 2024 17:38:20 +0100 Subject: [PATCH] feat(api): implemented pretty error responses --- src/api/endpoints/account/invite.rs | 8 +------- src/api/endpoints/account/mod.rs | 8 +------- src/backend/error.rs | 30 ++++++++++++++++++++++++++--- src/backend/mod.rs | 4 +++- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/api/endpoints/account/invite.rs b/src/api/endpoints/account/invite.rs index d73911d..170a4e8 100644 --- a/src/api/endpoints/account/invite.rs +++ b/src/api/endpoints/account/invite.rs @@ -18,13 +18,7 @@ pub async fn new(backend: web::Data, auth: BearerAuth) -> impl Responde HttpResponse::InternalServerError().finish() } Ok(res) => match res { - Err(e) => match e { - Error::PermissionDenied => HttpResponse::Unauthorized().finish(), - e => { - error!("!!! Error unknown to this context!!! -> {e}"); - HttpResponse::InternalServerError().finish() - } - }, + Err(e) => e.into(), Ok(uuid) => HttpResponse::Ok().json(NewResponse { token: uuid.to_string(), }), diff --git a/src/api/endpoints/account/mod.rs b/src/api/endpoints/account/mod.rs index 97bf99f..04fd96f 100644 --- a/src/api/endpoints/account/mod.rs +++ b/src/api/endpoints/account/mod.rs @@ -29,13 +29,7 @@ pub async fn register( HttpResponse::InternalServerError().finish() } Ok(res) => match res { - Err(e) => match e { - Error::InvalidToken => HttpResponse::Unauthorized().finish(), - e => { - error!("!!! Error unknown to this context!!! -> {e}"); - HttpResponse::InternalServerError().finish() - } - }, + Err(e) => e.into(), Ok(uuid) => HttpResponse::Ok().json(RegisterResponse { uuid: uuid.to_string(), }), diff --git a/src/backend/error.rs b/src/backend/error.rs index 116ea1e..12a4945 100644 --- a/src/backend/error.rs +++ b/src/backend/error.rs @@ -1,6 +1,8 @@ +use actix_web::HttpResponse; +use serde::Serialize; use thiserror::Error; -#[derive(Debug, Error)] +#[derive(Debug, Error, Serialize, Copy, Clone)] pub enum Error { #[error("The given token is invalid")] InvalidToken, @@ -8,9 +10,31 @@ pub enum Error { #[error("The given token is expired")] TokenExpired, - #[error("Permission denied")] - PermissionDenied, + #[error("Permission denied: {0}")] + PermissionDenied(&'static str), #[error("The given user cannot be found")] UserNotFound, } + +#[derive(Serialize)] +struct ErrorResponse { + error: Error, + description: String, +} + +impl Into for Error { + fn into(self) -> HttpResponse { + let body = ErrorResponse { + error: self, + description: self.to_string(), + }; + + match self { + Error::InvalidToken => HttpResponse::Unauthorized().json(body), + Error::TokenExpired => HttpResponse::Gone().json(body), + Error::PermissionDenied(_) => HttpResponse::Forbidden().json(body), + Error::UserNotFound => HttpResponse::NotFound().json(body), + } + } +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 9ef3326..693f56b 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -181,7 +181,9 @@ impl Backend { }; if !user.has_permission(Permission::GenerateInviteTokens) { - return Ok(Err(Error::PermissionDenied)); + return Ok(Err(Error::PermissionDenied( + "This user is not authorized to generate invite codes", + ))); } let token = rand::distributions::Alphanumeric.sample_string(&mut OsRng, 48);