diff --git a/src/crawler/mod.rs b/src/crawler/mod.rs index dcc173d..cac2a24 100644 --- a/src/crawler/mod.rs +++ b/src/crawler/mod.rs @@ -31,3 +31,13 @@ pub fn is_crawler(user_agent: &str) -> bool { .iter() .any(|crawler| crawler.pattern.is_match(user_agent)) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_is_crawler() { + assert_eq!(is_crawler("abc"), false); + } +} diff --git a/src/dao/mod.rs b/src/dao/mod.rs index c51f1d8..b939874 100644 --- a/src/dao/mod.rs +++ b/src/dao/mod.rs @@ -104,6 +104,10 @@ pub async fn get_link( .fetch_optional(pool) .await; + if matches!(result, Ok(None)) { + tracing::info!("No link found by id link_id={link_id}"); + } + result.map_err(|err| { tracing::error!("Error whilst fetching link link_id={link_id} err={err}"); Error::UnknownError(Box::new(err)) @@ -123,6 +127,10 @@ pub async fn get_link_by_link_path( .fetch_optional(pool) .await; + if matches!(result, Ok(None)) { + tracing::info!("No link found by url path link_path={link_path}"); + } + result.map_err(|err| { tracing::error!("Error whilst creating link err={err}"); Error::UnknownError(Box::new(err)) diff --git a/src/link_router/mod.rs b/src/link_router/mod.rs index f500d16..82b5936 100644 --- a/src/link_router/mod.rs +++ b/src/link_router/mod.rs @@ -77,31 +77,33 @@ async fn link_handler( ) -> impl IntoResponse { tracing::info!("Handling link link_path={link_path} request_actor={request_actor:?}"); - match dao::get_link_by_link_path(&app_context.pool, &link_path).await { - Ok(Some(link)) => match request_actor { - RequestActor::Crawler => { - let response = CrawlerResponseTemplate { - og_title: link.title, - og_description: link.description, - og_url: link.link_path, - og_image_url: link.image_url, - og_type: "website".to_string(), - }; - Html(response.render().unwrap()).into_response() + match request_actor { + RequestActor::Crawler => { + match dao::get_link_by_link_path(&app_context.pool, &link_path).await { + Ok(Some(link)) => { + let response = CrawlerResponseTemplate { + og_title: link.title, + og_description: link.description, + og_url: link.link_path, + og_image_url: link.image_url, + og_type: "website".to_string(), + }; + Html(response.render().unwrap()).into_response() + } + Ok(None) => Response::builder() + .status(StatusCode::NOT_FOUND) + .body("Link not found".to_string()), + Err(_) => Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body("Unknown error".to_string()), } - RequestActor::User(platform) => Redirect::temporary(match platform { - Platform::Android => &fallback_data.android_fallback, - Platform::Ios => &fallback_data.ios_fallback, - Platform::Web | Platform::Unknown => &fallback_data.web_fallback, - }) - .into_response(), - }, - Ok(None) => Response::builder() - .status(StatusCode::NOT_FOUND) - .body("Link not found".to_string()), - Err(_) => Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body("Unknown error".to_string()), + } + RequestActor::User(platform) => Redirect::temporary(match platform { + Platform::Android => &fallback_data.android_fallback, + Platform::Ios => &fallback_data.ios_fallback, + Platform::Web | Platform::Unknown => &fallback_data.web_fallback, + }) + .into_response(), } }