mirror of
https://github.com/moghtech/komodo.git
synced 2026-03-11 17:44:19 -05:00
periphery use AppError
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2178,7 +2178,7 @@ dependencies = [
|
||||
"run_command",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serror",
|
||||
"serror_axum",
|
||||
"simple_logger",
|
||||
"svi",
|
||||
"sysinfo",
|
||||
|
||||
@@ -27,7 +27,7 @@ termination_signal.workspace = true
|
||||
run_command.workspace = true
|
||||
svi.workspace = true
|
||||
resolver_api.workspace = true
|
||||
serror.workspace = true
|
||||
serror_axum.workspace = true
|
||||
# external
|
||||
tokio.workspace = true
|
||||
axum.workspace = true
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
use anyhow::anyhow;
|
||||
use async_timing_util::unix_timestamp_ms;
|
||||
use axum::http::StatusCode;
|
||||
use axum_extra::{headers::ContentType, TypedHeader};
|
||||
use monitor_client::entities::update::Log;
|
||||
use run_command::{async_run_command, CommandOutput};
|
||||
use serror::serialize_error;
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
@@ -71,13 +68,3 @@ pub fn output_into_log(
|
||||
end_ts: unix_timestamp_ms() as i64,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_response_error(
|
||||
e: anyhow::Error,
|
||||
) -> (StatusCode, TypedHeader<ContentType>, String) {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
TypedHeader(ContentType::json()),
|
||||
serialize_error(e),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,13 +4,11 @@ extern crate log;
|
||||
use std::{net::SocketAddr, sync::Arc, time::Instant};
|
||||
|
||||
use anyhow::Context;
|
||||
use axum::{
|
||||
http::StatusCode, middleware, routing::post, Extension, Json,
|
||||
Router,
|
||||
};
|
||||
use axum::{middleware, routing::post, Extension, Json, Router};
|
||||
|
||||
use axum_extra::{headers::ContentType, TypedHeader};
|
||||
use resolver_api::Resolver;
|
||||
use serror_axum::AppResult;
|
||||
use termination_signal::tokio::immediate_term_handle;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -23,11 +21,6 @@ mod state;
|
||||
use requests::PeripheryRequest;
|
||||
use state::State;
|
||||
|
||||
use crate::helpers::into_response_error;
|
||||
|
||||
type ResponseResult<T> =
|
||||
Result<T, (StatusCode, TypedHeader<ContentType>, String)>;
|
||||
|
||||
async fn app() -> anyhow::Result<()> {
|
||||
let state = State::load().await?;
|
||||
|
||||
@@ -45,22 +38,21 @@ async fn app() -> anyhow::Result<()> {
|
||||
let req_id = Uuid::new_v4();
|
||||
info!("request {req_id} | {request:?}");
|
||||
let res = tokio::spawn(async move {
|
||||
state.resolve_request(request, ()).await
|
||||
let res = state.resolve_request(request, ()).await;
|
||||
if let Err(e) = &res {
|
||||
debug!("request {req_id} ERROR: {e:#?}");
|
||||
}
|
||||
let elapsed = timer.elapsed();
|
||||
info!("request {req_id} | resolve time: {elapsed:?}");
|
||||
res
|
||||
})
|
||||
.await
|
||||
.context("failed in spawned request handler");
|
||||
.await;
|
||||
if let Err(e) = &res {
|
||||
debug!("request {req_id} SPAWN ERROR: {e:#?}");
|
||||
}
|
||||
let res = res.map_err(into_response_error)?;
|
||||
if let Err(e) = &res {
|
||||
debug!("request {req_id} ERROR: {e:#?}");
|
||||
}
|
||||
let res = res.map_err(into_response_error)?;
|
||||
let elapsed = timer.elapsed();
|
||||
info!("request {req_id} | resolve time: {elapsed:?}");
|
||||
let res = res??;
|
||||
debug!("request {req_id} RESPONSE: {res}");
|
||||
ResponseResult::Ok((TypedHeader(ContentType::json()), res))
|
||||
AppResult::Ok((TypedHeader(ContentType::json()), res))
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user