chore: better error type prototype
This commit is contained in:
parent
80675338af
commit
373eae1b72
2 changed files with 19 additions and 2 deletions
17
src/proto.rs
17
src/proto.rs
|
@ -1,8 +1,11 @@
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use axum::{Json, http::StatusCode};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
pub type Response<T> = Result<Json<T>, (StatusCode, Json<Error>)>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
pub error: String,
|
pub error: String,
|
||||||
|
@ -10,6 +13,20 @@ pub struct Error {
|
||||||
pub cause: String,
|
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)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
|
@ -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);
|
info!(target: "AUTH", "[REFRESH] called with {:?}", payload);
|
||||||
let token = entities::token::Entity::find().filter(
|
let token = entities::token::Entity::find().filter(
|
||||||
entities::token::Column::AccessToken.eq(payload.accessToken.clone())
|
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);
|
info!(target: "AUTH", "[REFRESH] answering with {:?}", response);
|
||||||
Ok(Json(response))
|
Ok(Json(response))
|
||||||
} else {
|
} else {
|
||||||
Err(StatusCode::UNAUTHORIZED)
|
Err((StatusCode::UNAUTHORIZED, Json(proto::Error::simple("invalid token"))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue