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()
|
HttpResponse::InternalServerError().finish()
|
||||||
}
|
}
|
||||||
Ok(res) => match res {
|
Ok(res) => match res {
|
||||||
Err(e) => match e {
|
Err(e) => e.into(),
|
||||||
Error::PermissionDenied => HttpResponse::Unauthorized().finish(),
|
|
||||||
e => {
|
|
||||||
error!("!!! Error unknown to this context!!! -> {e}");
|
|
||||||
HttpResponse::InternalServerError().finish()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Ok(uuid) => HttpResponse::Ok().json(NewResponse {
|
Ok(uuid) => HttpResponse::Ok().json(NewResponse {
|
||||||
token: uuid.to_string(),
|
token: uuid.to_string(),
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -29,13 +29,7 @@ pub async fn register(
|
||||||
HttpResponse::InternalServerError().finish()
|
HttpResponse::InternalServerError().finish()
|
||||||
}
|
}
|
||||||
Ok(res) => match res {
|
Ok(res) => match res {
|
||||||
Err(e) => match e {
|
Err(e) => e.into(),
|
||||||
Error::InvalidToken => HttpResponse::Unauthorized().finish(),
|
|
||||||
e => {
|
|
||||||
error!("!!! Error unknown to this context!!! -> {e}");
|
|
||||||
HttpResponse::InternalServerError().finish()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Ok(uuid) => HttpResponse::Ok().json(RegisterResponse {
|
Ok(uuid) => HttpResponse::Ok().json(RegisterResponse {
|
||||||
uuid: uuid.to_string(),
|
uuid: uuid.to_string(),
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
use actix_web::HttpResponse;
|
||||||
|
use serde::Serialize;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error, Serialize, Copy, Clone)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("The given token is invalid")]
|
#[error("The given token is invalid")]
|
||||||
InvalidToken,
|
InvalidToken,
|
||||||
|
@ -8,9 +10,31 @@ pub enum Error {
|
||||||
#[error("The given token is expired")]
|
#[error("The given token is expired")]
|
||||||
TokenExpired,
|
TokenExpired,
|
||||||
|
|
||||||
#[error("Permission denied")]
|
#[error("Permission denied: {0}")]
|
||||||
PermissionDenied,
|
PermissionDenied(&'static str),
|
||||||
|
|
||||||
#[error("The given user cannot be found")]
|
#[error("The given user cannot be found")]
|
||||||
UserNotFound,
|
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) {
|
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);
|
let token = rand::distributions::Alphanumeric.sample_string(&mut OsRng, 48);
|
||||||
|
|
Loading…
Reference in New Issue