Skip to content

Commit

Permalink
chore: update error funcs
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss committed May 27, 2024
1 parent 1f3562e commit b6dc087
Showing 1 changed file with 7 additions and 80 deletions.
87 changes: 7 additions & 80 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
use crate::utils::{LogLevel, NewLogRecord};
use hyper::{body::HttpBody, Body, Response};
use serde_json::json;
use hyper::{Body, Response};
use thiserror::Error;

#[allow(dead_code)]
pub(crate) fn not_implemented() -> Response<Body> {
// log error
{
let record = NewLogRecord::new(
LogLevel::Error,
None,
json!({
"message": "501 Not Implemented",
}),
);
let message = serde_json::to_string(&record).unwrap();
error!("{}", &message);
}
error!(target: "response", "501 Not Implemented");

Response::builder()
.header("Access-Control-Allow-Origin", "*")
Expand All @@ -34,56 +22,15 @@ pub(crate) fn internal_server_error(msg: impl AsRef<str>) -> Response<Body> {
};

// log error
{
let record = NewLogRecord::new(
LogLevel::Error,
None,
json!({
"message": format!("{msg}", msg = err_msg),
}),
);
let message = serde_json::to_string(&record).unwrap();
error!("{}", &message);
}
error!(target: "response", "{}", &err_msg);

let response = Response::builder()
Response::builder()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "*")
.header("Access-Control-Allow-Headers", "*")
.status(hyper::StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(err_msg))
.unwrap();

// log
{
let status_code = response.status();
let response_version = format!("{:?}", response.version());
let response_body_size: u64 = response.body().size_hint().lower();
let response_status = status_code.as_u16();
let response_is_informational = status_code.is_informational();
let response_is_success = status_code.is_success();
let response_is_redirection = status_code.is_redirection();
let response_is_client_error = status_code.is_client_error();
let response_is_server_error = status_code.is_server_error();
let record = NewLogRecord::new(
LogLevel::Error,
None,
json!({
"version": response_version,
"body_size": response_body_size,
"status": response_status,
"is_informational": response_is_informational,
"is_success": response_is_success,
"is_redirection": response_is_redirection,
"is_client_error": response_is_client_error,
"is_server_error": response_is_server_error,
}),
);
let message = serde_json::to_string(&record).unwrap();
error!(target: "response", "{}", message);
}

response
.unwrap()
}

pub(crate) fn bad_request(msg: impl AsRef<str>) -> Response<Body> {
Expand All @@ -93,17 +40,7 @@ pub(crate) fn bad_request(msg: impl AsRef<str>) -> Response<Body> {
};

// log error
{
let record = NewLogRecord::new(
LogLevel::Error,
None,
json!({
"message": format!("{msg}", msg = err_msg),
}),
);
let message = serde_json::to_string(&record).unwrap();
error!("{}", &message);
}
error!(target: "response", "{}", &err_msg);

Response::builder()
.header("Access-Control-Allow-Origin", "*")
Expand All @@ -124,17 +61,7 @@ pub(crate) fn invalid_endpoint(msg: impl AsRef<str>) -> Response<Body> {
};

// log error
{
let record = NewLogRecord::new(
LogLevel::Error,
None,
json!({
"message": format!("{msg}", msg = err_msg),
}),
);
let message = serde_json::to_string(&record).unwrap();
error!("{}", &message);
}
error!(target: "response", "{}", &err_msg);

Response::builder()
.header("Access-Control-Allow-Origin", "*")
Expand Down

0 comments on commit b6dc087

Please sign in to comment.