From 373eae1b72ffa0f2d6cf8107027c87473a2da2d8 Mon Sep 17 00:00:00 2001 From: "dev@ftbsc" Date: Sun, 22 Jan 2023 22:35:47 +0100 Subject: [PATCH] chore: better error type prototype --- src/proto.rs | 17 +++++++++++++++++ src/routes/auth.rs | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/proto.rs b/src/proto.rs index 4b3b8bd..ab5ab62 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -1,8 +1,11 @@ #![allow(non_snake_case)] +use axum::{Json, http::StatusCode}; use uuid::Uuid; use serde::{Serialize, Deserialize}; +pub type Response = Result, (StatusCode, Json)>; + #[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 { + Json(self) + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct Agent { pub name: String, diff --git a/src/routes/auth.rs b/src/routes/auth.rs index da49d18..a6077aa 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -21,7 +21,7 @@ pub async fn validate(State(state): State, Json(payload): Json, Json(payload): Json) -> Result, StatusCode> { +pub async fn refresh(State(state): State, Json(payload): Json) -> proto::Response { 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, Json(payload): Json