chore: better error type prototype

This commit is contained in:
dev@ftbsc 2023-01-22 22:35:47 +01:00
parent 80675338af
commit 373eae1b72
2 changed files with 19 additions and 2 deletions

View file

@ -1,8 +1,11 @@
#![allow(non_snake_case)]
use axum::{Json, http::StatusCode};
use uuid::Uuid;
use serde::{Serialize, Deserialize};
pub type Response<T> = Result<Json<T>, (StatusCode, Json<Error>)>;
#[derive(Serialize, Deserialize)]
pub struct Error {
pub error: String,
@ -10,6 +13,20 @@ pub struct Error {
pub cause: String,
}
impl Error {
pub fn simple(msg: impl ToString) -> Error {
Error {
error: msg.to_string(),
errorMessage: msg.to_string(),
cause: msg.to_string(),
}
}
pub fn json(self) -> Json<Self> {
Json(self)
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Agent {
pub name: String,

View file

@ -21,7 +21,7 @@ pub async fn validate(State(state): State<AppState>, Json(payload): Json<proto::
}
}
pub async fn refresh(State(state): State<AppState>, Json(payload): Json<proto::RefreshRequest>) -> Result<Json<proto::RefreshResponse>, StatusCode> {
pub async fn refresh(State(state): State<AppState>, Json(payload): Json<proto::RefreshRequest>) -> proto::Response<proto::RefreshResponse> {
info!(target: "AUTH", "[REFRESH] called with {:?}", payload);
let token = entities::token::Entity::find().filter(
entities::token::Column::AccessToken.eq(payload.accessToken.clone())
@ -49,7 +49,7 @@ pub async fn refresh(State(state): State<AppState>, Json(payload): Json<proto::R
info!(target: "AUTH", "[REFRESH] answering with {:?}", response);
Ok(Json(response))
} else {
Err(StatusCode::UNAUTHORIZED)
Err((StatusCode::UNAUTHORIZED, Json(proto::Error::simple("invalid token"))))
}
}