feat(api): implemented pretty error responses
This commit is contained in:
parent
68b082efe5
commit
6a739e2dad
|
@ -18,13 +18,7 @@ pub async fn new(backend: web::Data<Backend>, 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(),
|
||||
}),
|
||||
|
|
|
@ -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(),
|
||||
}),
|
||||
|
|
|
@ -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<HttpResponse> 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue