Skip to content

Commit

Permalink
Add http business status code for asynchronous processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
gudaoxuri committed Aug 16, 2022
1 parent c29844d commit 2fb5f21
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/basic/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ use core::result::Result;
pub type TardisResult<T> = Result<T, TardisError>;

pub const TARDIS_RESULT_SUCCESS_CODE: &str = "200";
pub const TARDIS_RESULT_ACCEPTED_CODE: &str = "202";
12 changes: 10 additions & 2 deletions src/web/web_resp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::basic::error::TardisError;
use crate::basic::result::TARDIS_RESULT_SUCCESS_CODE;
use crate::basic::result::{TARDIS_RESULT_ACCEPTED_CODE, TARDIS_RESULT_SUCCESS_CODE};
use crate::serde::{Deserialize, Serialize};
use crate::TardisFuns;
use poem::http::StatusCode;
Expand Down Expand Up @@ -82,7 +82,7 @@ pub fn mapping_http_code_to_error(http_code: StatusCode, msg: &str) -> Option<Ta
return Some(error);
}
match http_code {
StatusCode::OK => None,
code if code.as_u16() < 400 => None,
_ => Some(TardisError::custom(
http_code.as_str(),
&format!("[Tardis.WebServer] Process error: {}", msg),
Expand Down Expand Up @@ -120,6 +120,14 @@ where
}))
}

pub fn accepted(data: T) -> TardisApiResult<T> {
TardisApiResult::Ok(Json(TardisResp {
code: TARDIS_RESULT_ACCEPTED_CODE.to_string(),
msg: "".to_string(),
data: Some(data),
}))
}

pub fn err(error: TardisError) -> TardisApiResult<T> {
TardisApiResult::Err(error.into())
}
Expand Down
20 changes: 16 additions & 4 deletions tests/test_web_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tardis::basic::config::{CacheConfig, DBConfig, FrameworkConfig, MQConfig, Ma
use tardis::basic::dto::TardisContext;
use tardis::basic::error::TardisError;
use tardis::basic::field::TrimString;
use tardis::basic::result::{TardisResult, TARDIS_RESULT_SUCCESS_CODE};
use tardis::basic::result::{TardisResult, TARDIS_RESULT_ACCEPTED_CODE, TARDIS_RESULT_SUCCESS_CODE};
use tardis::serde::{Deserialize, Serialize};
use tardis::test::test_container::TardisTestContainer;
use tardis::web::context_extractor::{TardisContextExtractor, TOKEN_FLAG};
Expand Down Expand Up @@ -162,9 +162,16 @@ async fn start_serv(web_url: &str, redis_url: &str) -> TardisResult<()> {

async fn test_basic(url: &str) -> TardisResult<()> {
// Normal
let response = TardisFuns::web_client().get::<TardisResp<TodoResp>>(format!("{}/todo/todos/1", url).as_str(), None).await?.body.unwrap();
assert_eq!(response.code, TARDIS_RESULT_SUCCESS_CODE);
assert_eq!(response.data.unwrap().code.to_string(), "code1");
let response = TardisFuns::web_client().get::<TardisResp<TodoResp>>(format!("{}/todo/todos/1", url).as_str(), None).await?;
assert_eq!(response.code, 200);
assert_eq!(response.body.as_ref().unwrap().code, TARDIS_RESULT_SUCCESS_CODE);
assert_eq!(response.body.as_ref().unwrap().data.as_ref().unwrap().code.to_string(), "code1");

// Accepted
let response = TardisFuns::web_client().get::<TardisResp<String>>(format!("{}/todo/todos/1/async", url).as_str(), None).await?;
assert_eq!(response.code, 200);
assert_eq!(response.body.as_ref().unwrap().code, TARDIS_RESULT_ACCEPTED_CODE);
assert_eq!(response.body.as_ref().unwrap().data.as_ref().unwrap(), "/todos/1/status");

// Business Error
let response = TardisFuns::web_client().get::<TardisResp<TodoResp>>(format!("{}/todo/todos/1/err", url).as_str(), None).await?.body.unwrap();
Expand Down Expand Up @@ -707,6 +714,11 @@ impl TodosApi {
})
}

#[oai(path = "/todos/:id/async", method = "get")]
async fn get_async(&self, id: Path<i64>) -> TardisApiResult<String> {
TardisResp::accepted(format!("/todos/{}/status", id.0))
}

#[oai(path = "/todos/:id/err", method = "get")]
async fn get_by_error(&self, id: Path<i64>) -> TardisApiResult<TodoResp> {
TardisResp::err(TardisError::conflict("异常", ""))
Expand Down

0 comments on commit 2fb5f21

Please sign in to comment.