Quiet requests logging, forward context info in error

This commit is contained in:
asonix 2020-04-21 16:39:02 -05:00
parent 11f56b56e9
commit b8336b441c
2 changed files with 15 additions and 26 deletions

View file

@ -86,11 +86,11 @@ pub enum MyError {
#[error("Object has already been relayed")] #[error("Object has already been relayed")]
Duplicate, Duplicate,
#[error("Couldn't send request")] #[error("Couldn't send request to {0}, {1}")]
SendRequest, SendRequest(String, String),
#[error("Couldn't receive request response")] #[error("Couldn't receive request response from {0}, {1}")]
ReceiveResponse, ReceiveResponse(String, String),
#[error("Response has invalid status code, {0}")] #[error("Response has invalid status code, {0}")]
Status(StatusCode), Status(StatusCode),

View file

@ -3,7 +3,7 @@ use activitystreams::primitives::XsdAnyUri;
use actix_web::client::Client; use actix_web::client::Client;
use bytes::Bytes; use bytes::Bytes;
use http_signature_normalization_actix::prelude::*; use http_signature_normalization_actix::prelude::*;
use log::{error, info}; use log::{debug, info};
use rsa::{hash::Hashes, padding::PaddingScheme, RSAPrivateKey}; use rsa::{hash::Hashes, padding::PaddingScheme, RSAPrivateKey};
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
@ -43,16 +43,13 @@ impl Requests {
.await? .await?
.send() .send()
.await .await
.map_err(|e| { .map_err(|e| MyError::SendRequest(url.to_string(), e.to_string()))?;
error!("Couldn't send request to {}, {}", url, e);
MyError::SendRequest
})?;
if !res.status().is_success() { if !res.status().is_success() {
if let Ok(bytes) = res.body().await { if let Ok(bytes) = res.body().await {
if let Ok(s) = String::from_utf8(bytes.as_ref().to_vec()) { if let Ok(s) = String::from_utf8(bytes.as_ref().to_vec()) {
if !s.is_empty() { if !s.is_empty() {
error!("Response from {}, {}", url, s); debug!("Response from {}, {}", url, s);
} }
} }
} }
@ -60,10 +57,9 @@ impl Requests {
return Err(MyError::Status(res.status())); return Err(MyError::Status(res.status()));
} }
res.json().await.map_err(|e| { res.json()
error!("Coudn't fetch json from {}, {}", url, e); .await
MyError::ReceiveResponse .map_err(|e| MyError::ReceiveResponse(url.to_string(), e.to_string()))
})
} }
pub async fn fetch_bytes(&self, url: &str) -> Result<(String, Bytes), MyError> { pub async fn fetch_bytes(&self, url: &str) -> Result<(String, Bytes), MyError> {
@ -82,10 +78,7 @@ impl Requests {
.await? .await?
.send() .send()
.await .await
.map_err(|e| { .map_err(|e| MyError::SendRequest(url.to_string(), e.to_string()))?;
error!("Couldn't send request to {}, {}", url, e);
MyError::SendRequest
})?;
let content_type = if let Some(content_type) = res.headers().get("content-type") { let content_type = if let Some(content_type) = res.headers().get("content-type") {
if let Ok(s) = content_type.to_str() { if let Ok(s) = content_type.to_str() {
@ -101,7 +94,7 @@ impl Requests {
if let Ok(bytes) = res.body().await { if let Ok(bytes) = res.body().await {
if let Ok(s) = String::from_utf8(bytes.as_ref().to_vec()) { if let Ok(s) = String::from_utf8(bytes.as_ref().to_vec()) {
if !s.is_empty() { if !s.is_empty() {
error!("Response from {}, {}", url, s); debug!("Response from {}, {}", url, s);
} }
} }
} }
@ -111,8 +104,7 @@ impl Requests {
let bytes = match res.body().limit(1024 * 1024 * 4).await { let bytes = match res.body().limit(1024 * 1024 * 4).await {
Err(e) => { Err(e) => {
error!("Coudn't fetch json from {}, {}", url, e); return Err(MyError::ReceiveResponse(url.to_string(), e.to_string()));
return Err(MyError::ReceiveResponse);
} }
Ok(bytes) => bytes, Ok(bytes) => bytes,
}; };
@ -142,16 +134,13 @@ impl Requests {
.await? .await?
.send() .send()
.await .await
.map_err(|e| { .map_err(|e| MyError::SendRequest(inbox.to_string(), e.to_string()))?;
error!("Couldn't send deliver request to {}, {}", inbox, e);
MyError::SendRequest
})?;
if !res.status().is_success() { if !res.status().is_success() {
if let Ok(bytes) = res.body().await { if let Ok(bytes) = res.body().await {
if let Ok(s) = String::from_utf8(bytes.as_ref().to_vec()) { if let Ok(s) = String::from_utf8(bytes.as_ref().to_vec()) {
if !s.is_empty() { if !s.is_empty() {
error!("Response from {}, {}", inbox.as_str(), s); debug!("Response from {}, {}", inbox.as_str(), s);
} }
} }
} }