Skip to content

Commit

Permalink
fix: wrongly unified status code
Browse files Browse the repository at this point in the history
  • Loading branch information
4t145 committed Jan 13, 2025
1 parent 0058079 commit aeb4c85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
37 changes: 27 additions & 10 deletions tardis/src/web/uniform_error_mw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::basic::error::TardisError;
use crate::basic::result::TARDIS_RESULT_SUCCESS_CODE;
use crate::serde_json::json;
use crate::web::web_resp::HEADER_X_TARDIS_ERROR;
use crate::TardisFuns;
use http::header::CONTENT_TYPE;
use poem::http::StatusCode;
use poem::{Endpoint, IntoResponse, Middleware, Request, Response};
use tracing::{trace, warn};
Expand Down Expand Up @@ -75,20 +77,35 @@ impl<E: Endpoint> Endpoint for UniformErrorImpl<E> {
Ok(resp)
}
Err(error) => {
let msg = error.to_string();
let error = mapping_http_code_to_error(error.into_response().status(), &msg)
.ok_or_else(|| TardisError::internal_error(&format!("[Tardis.WebServer] {msg} cannot be mapped into http error code"), "500-tardis-webserver-error"))?;
let error = if error.has_source() {
// ?????? unbelievably ridiculous
let msg = error.to_string();
mapping_http_code_to_error(error.into_response().status(), &msg)
.ok_or_else(|| TardisError::internal_error(&format!("[Tardis.WebServer] {msg} cannot be mapped into http error code"), "500-tardis-webserver-error"))?
} else {
// I don't know how to handle this
let mut raw_response = error.into_response();
let response_body_str = raw_response.take_body().into_string().await?;
mapping_http_code_to_error(raw_response.status(), &response_body_str).ok_or_else(|| {
TardisError::internal_error(
&format!("[Tardis.WebServer] {response_body_str} cannot be mapped into http error code"),
"500-tardis-webserver-error",
)
})?
};
warn!(
"[Tardis.WebServer] Process error,request method:{}, url:{}, response code:{}, message:{}",
method, url, error.code, error.message
);
Ok(Response::builder().status(StatusCode::OK).header("Content-Type", "application/json; charset=utf8").body(
json!({
"code": error.code,
"msg": process_err_msg(error.code.as_str(),error.message),
})
.to_string(),
))
Ok(
Response::builder().status(StatusCode::OK).header(CONTENT_TYPE, "application/json; charset=utf8").header(HEADER_X_TARDIS_ERROR, &error.code).body(
json!({
"code": error.code,
"msg": process_err_msg(error.code.as_str(), error.message),
})
.to_string(),
),
)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions tardis/src/web/web_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ impl From<TardisError> for poem::Error {
}

pub fn mapping_http_code_to_error(http_code: StatusCode, msg: &str) -> Option<TardisError> {
if msg.starts_with(TARDIS_ERROR_FLAG) {
let msg = msg.split_at(TARDIS_ERROR_FLAG.len()).1.to_string();
let error = TardisFuns::json.str_to_obj(&msg).unwrap_or_else(|_| TardisError::format_error("[Tardis.WebServer] Invalid format error", "406-tardis-error-invalid"));
if let Some(tardis_error) = msg.strip_prefix(TARDIS_ERROR_FLAG) {
let error = TardisFuns::json.str_to_obj(tardis_error).unwrap_or_else(|_| TardisError::format_error("[Tardis.WebServer] Invalid format error", "406-tardis-error-invalid"));
return Some(error);
}
match http_code {
Expand Down

0 comments on commit aeb4c85

Please sign in to comment.