Skip to content

Commit

Permalink
err log print
Browse files Browse the repository at this point in the history
  • Loading branch information
hongcha98 committed May 12, 2024
1 parent abacdaf commit 28ef602
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
54 changes: 40 additions & 14 deletions gateway/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use live777_http::{
request::{QueryInfo, Reforward},
response::StreamInfo,
};
use reqwest::{header::HeaderMap, Body, Method};
use reqwest::{header::HeaderMap, Method};
use serde::{Deserialize, Serialize};
use sqlx::prelude::FromRow;
use tracing::{debug, warn};

#[derive(Serialize, Deserialize, Clone, Debug, FromRow)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -139,7 +140,7 @@ impl Node {
self.path_url(&path::infos(QueryInfo { streams })),
"GET",
self.admin_authorization.clone(),
"",
"".to_string(),
)
.await?;
serde_json::from_str::<Vec<StreamInfo>>(&data).map_err(|e| e.into())
Expand Down Expand Up @@ -169,18 +170,18 @@ impl Node {
self.path_url(&path::resource(&stream, &session)),
"DELETE",
self.admin_authorization.clone(),
"",
"".to_string(),
)
.await?;
Ok(())
}
}

async fn request<T: Into<Body>>(
async fn request(
url: String,
method: &str,
authorization: Option<String>,
body: T,
body: String,
) -> Result<String> {
let mut headers = HeaderMap::new();
headers.append("Content-Type", "application/json".parse().unwrap());
Expand All @@ -191,16 +192,41 @@ async fn request<T: Into<Body>>(
.connect_timeout(Duration::from_millis(500))
.timeout(Duration::from_millis(5000))
.build()?;
let response = client
.request(Method::from_str(method)?, url)

match client
.request(Method::from_str(method)?, url.clone())
.headers(headers)
.body(body)
.body(body.clone())
.send()
.await?;
let success = response.status().is_success();
let body = response.text().await?;
if !success {
return Err(AppError::InternalServerError(anyhow!(body)));
.await
{
Ok(response) => {
let status = response.status();
let success = response.status().is_success();
let res_body = response.text().await?;
if success {
debug!(
url,
?status,
req_body = body,
res_body,
"request node success"
);
Ok(res_body)
} else {
warn!(
url,
?status,
req_body = body,
res_body,
"request node error"
);
Err(AppError::InternalServerError(anyhow!(res_body)))
}
}
Err(err) => {
warn!(url, req_body = body, ?err, "request node error");
Err(err.into())
}
}
Ok(body)
}
26 changes: 20 additions & 6 deletions src/hook/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_trait::async_trait;
use live777_http::event::{EventBody, NodeMetaData, NodeMetrics};
use reqwest::{header::HeaderMap, Client, Method};
use tokio::sync::broadcast;
use tracing::debug;
use tracing::{debug, warn};

use super::{Event, EventHook, NodeEvent};
use crate::{error::AppError, metrics, result::Result};
Expand Down Expand Up @@ -51,17 +51,31 @@ impl WebHook {
.await
{
Ok(response) => {
let status = response.status();
let success = response.status().is_success();
let res_body = response.text().await?;
debug!(url = self.url, req_body, success, res_body, "event webhook");
if !success {
Err(AppError::throw(res_body))
} else {
if success {
debug!(
url = self.url,
?status,
req_body,
res_body,
"event webhook success"
);
Ok(())
} else {
warn!(
url = self.url,
?status,
req_body,
res_body,
"event webhook error"
);
Err(AppError::throw(res_body))
}
}
Err(err) => {
debug!(url = self.url, req_body, ?err, "event webhook error");
warn!(url = self.url, req_body, ?err, "event webhook error");
Err(err.into())
}
}
Expand Down

0 comments on commit 28ef602

Please sign in to comment.