Add better logging

This commit is contained in:
Leonora Tindall 2022-11-03 23:30:59 -05:00
parent 6e782a72f7
commit 81890b7d7d
Signed by: nora
GPG Key ID: 7A8B52EC67E09AAF
1 changed files with 20 additions and 9 deletions

View File

@ -124,7 +124,7 @@ async fn get_full_post_data(project_id: String) -> Result<CohostPostsPage, Error
// Not cached because it's never used individually. // Not cached because it's never used individually.
async fn get_page_data(project_id: String, page: u64) -> Result<CohostPostsPage, ErrorResponse> { async fn get_page_data(project_id: String, page: u64) -> Result<CohostPostsPage, ErrorResponse> {
let posts_url = cohost_posts_api_url(&project_id, page); let posts_url = cohost_posts_api_url(&project_id, page);
eprintln!("making request to {}", posts_url); eprintln!("[INT] making request to {}", posts_url);
match CLIENT.get(posts_url).send().await { match CLIENT.get(posts_url).send().await {
Ok(v) => match v.status() { Ok(v) => match v.status() {
StatusCode::OK => match v.json::<CohostPostsPage>().await { StatusCode::OK => match v.json::<CohostPostsPage>().await {
@ -134,14 +134,14 @@ async fn get_page_data(project_id: String, page: u64) -> Result<CohostPostsPage,
"Couldn't deserialize Cohost posts page for '{}': {:?}", "Couldn't deserialize Cohost posts page for '{}': {:?}",
project_id, e project_id, e
); );
eprintln!("{}", err); eprintln!("[ERR] {}", err);
return Err(ErrorResponse::InternalError(err)); return Err(ErrorResponse::InternalError(err));
} }
}, },
// TODO NORA: Handle possible redirects // TODO NORA: Handle possible redirects
s => { s => {
let err = format!("Didn't receive status code 200 for posts for Cohost project '{}'; got {:?} instead.", page, s); let err = format!("Didn't receive status code 200 for posts for Cohost project '{}'; got {:?} instead.", page, s);
eprintln!("{}", err); eprintln!("[ERR] {}", err);
return Err(ErrorResponse::NotFound(err)); return Err(ErrorResponse::NotFound(err));
} }
}, },
@ -150,7 +150,7 @@ async fn get_page_data(project_id: String, page: u64) -> Result<CohostPostsPage,
"Error making request to Cohost for posts for project '{}': {:?}", "Error making request to Cohost for posts for project '{}': {:?}",
project_id, e project_id, e
); );
eprintln!("{}", err); eprintln!("[ERR] {}", err);
return Err(ErrorResponse::InternalError(err)); return Err(ErrorResponse::InternalError(err));
} }
} }
@ -159,7 +159,7 @@ async fn get_page_data(project_id: String, page: u64) -> Result<CohostPostsPage,
#[cached(time = 60, result)] #[cached(time = 60, result)]
async fn get_project_data(project_id: String) -> Result<CohostAccount, ErrorResponse> { async fn get_project_data(project_id: String) -> Result<CohostAccount, ErrorResponse> {
let project_url = format!("{}{}", COHOST_ACCOUNT_API_URL, project_id); let project_url = format!("{}{}", COHOST_ACCOUNT_API_URL, project_id);
eprintln!("making request to {}", project_url); eprintln!("[INT] making request to {}", project_url);
match CLIENT.get(project_url).send().await { match CLIENT.get(project_url).send().await {
Ok(v) => match v.status() { Ok(v) => match v.status() {
StatusCode::OK => match v.json::<CohostAccount>().await { StatusCode::OK => match v.json::<CohostAccount>().await {
@ -169,7 +169,7 @@ async fn get_project_data(project_id: String) -> Result<CohostAccount, ErrorResp
"Couldn't deserialize Cohost project '{}': {:?}", "Couldn't deserialize Cohost project '{}': {:?}",
project_id, e project_id, e
); );
eprintln!("{}", err); eprintln!("[ERR] {}", err);
Err(ErrorResponse::InternalError(err)) Err(ErrorResponse::InternalError(err))
} }
}, },
@ -179,7 +179,7 @@ async fn get_project_data(project_id: String) -> Result<CohostAccount, ErrorResp
"Didn't receive status code 200 for Cohost project '{}'; got {:?} instead.", "Didn't receive status code 200 for Cohost project '{}'; got {:?} instead.",
project_id, s project_id, s
); );
eprintln!("{}", err); eprintln!("[ERR] {}", err);
Err(ErrorResponse::NotFound(err)) Err(ErrorResponse::NotFound(err))
} }
}, },
@ -188,7 +188,7 @@ async fn get_project_data(project_id: String) -> Result<CohostAccount, ErrorResp
"Error making request to Cohost for project '{}': {:?}", "Error making request to Cohost for project '{}': {:?}",
project_id, e project_id, e
); );
eprintln!("{}", err); eprintln!("[ERR] {}", err);
Err(ErrorResponse::InternalError(err)) Err(ErrorResponse::InternalError(err))
} }
} }
@ -196,6 +196,7 @@ async fn get_project_data(project_id: String) -> Result<CohostAccount, ErrorResp
#[get("/<project>/originals.rss")] #[get("/<project>/originals.rss")]
async fn syndication_originals_rss_route(project: String) -> Result<RssResponse, ErrorResponse> { async fn syndication_originals_rss_route(project: String) -> Result<RssResponse, ErrorResponse> {
eprintln!("[EXT] Request to /{}/originals.rss", project);
let project_data = get_project_data(project.clone()).await?; let project_data = get_project_data(project.clone()).await?;
let page_data = get_full_post_data(project.clone()).await?; let page_data = get_full_post_data(project.clone()).await?;
Ok(RssResponse { Ok(RssResponse {
@ -206,6 +207,7 @@ async fn syndication_originals_rss_route(project: String) -> Result<RssResponse,
#[get("/<project>/feed.rss")] #[get("/<project>/feed.rss")]
async fn syndication_rss_route(project: String) -> Result<RssResponse, ErrorResponse> { async fn syndication_rss_route(project: String) -> Result<RssResponse, ErrorResponse> {
eprintln!("[EXT] Request to /{}/feed.rss", project);
let project_data = get_project_data(project.clone()).await?; let project_data = get_project_data(project.clone()).await?;
let page_data = get_full_post_data(project.clone()).await?; let page_data = get_full_post_data(project.clone()).await?;
Ok(RssResponse { Ok(RssResponse {
@ -216,6 +218,7 @@ async fn syndication_rss_route(project: String) -> Result<RssResponse, ErrorResp
#[get("/<project>/<id>")] #[get("/<project>/<id>")]
async fn post_md_route(project: String, id: u64) -> Result<MdResponse, ErrorResponse> { async fn post_md_route(project: String, id: u64) -> Result<MdResponse, ErrorResponse> {
eprintln!("[EXT] Request to /{}/{}", project, id);
let _project_data = get_project_data(project.clone()).await?; let _project_data = get_project_data(project.clone()).await?;
let post_data = get_post_from_page(project.clone(), id).await?; let post_data = get_post_from_page(project.clone(), id).await?;
Ok(MdResponse { Ok(MdResponse {
@ -227,12 +230,20 @@ async fn post_md_route(project: String, id: u64) -> Result<MdResponse, ErrorResp
async fn webfinger_route( async fn webfinger_route(
params: HashMap<String, String>, params: HashMap<String, String>,
) -> Result<Json<CohostWebfingerResource>, ErrorResponse> { ) -> Result<Json<CohostWebfingerResource>, ErrorResponse> {
let mut url_params_string = String::new();
for (k, v) in params.iter() {
url_params_string.push_str(&format!("{}={}&", k, v));
}
eprintln!(
"[EXT] Request to /.well_known/webfinger?{}",
url_params_string
);
if params.len() != 1 { if params.len() != 1 {
let err = format!( let err = format!(
"Too may or too few parameters. Expected 1, got {}", "Too may or too few parameters. Expected 1, got {}",
params.len() params.len()
); );
eprintln!("{}", err); eprintln!("[ERR] {}", err);
return Err(ErrorResponse::InternalError(err)); return Err(ErrorResponse::InternalError(err));
} }
if let Some(param) = params.iter().next() { if let Some(param) = params.iter().next() {