From f200b1265f62a1d4c91dbeeb7100dd79afb261fa Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 15 May 2022 01:04:55 +0200 Subject: refactor(tazjin/tgsa): move error handling one layer up this lays the groundwork for adding another handler and returning handler results as `anyhow::Result`. needed for the image redirect stuff. Change-Id: I909bd9c2f46f42ea759d50662d7bc36c1f408ed3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5609 Tested-by: BuildkiteCI Reviewed-by: tazjin --- users/tazjin/tgsa/src/main.rs | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'users/tazjin') diff --git a/users/tazjin/tgsa/src/main.rs b/users/tazjin/tgsa/src/main.rs index 911f1bad6788..0d323bb5be4a 100644 --- a/users/tazjin/tgsa/src/main.rs +++ b/users/tazjin/tgsa/src/main.rs @@ -221,9 +221,9 @@ fn fetch_with_cache(cache: &Cache, link: &TgLink) -> Result { Ok(post) } -fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result { +fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result { let post = fetch_with_cache(cache, link)?; - Ok(post.bbcode) + Ok(rouille::Response::text(post.bbcode)) } fn main() { @@ -232,9 +232,10 @@ fn main() { let cache: Cache = RwLock::new(HashMap::new()); rouille::start_server("0.0.0.0:8472", move |request| { - match TgLink::parse(request.raw_url()) { - None => rouille::Response::text( - r#"tgsa + let response = { + match TgLink::parse(request.raw_url()) { + None => Ok(rouille::Response::text( + r#"tgsa ---- this is a stupid program that lets you turn telegram message links @@ -255,22 +256,22 @@ didn't. try again. idiot. pm me on the forums if this makes you mad or something. "#, - ), - Some(link) => { - let result = handle_tg_link(&cache, &link); - match result { - Ok(bbcode) => rouille::Response::text(bbcode), - Err(err) => { - println!("something failed: {}", err); - rouille::Response::text(format!( - r#"something broke: {} + )), + Some(link) => handle_tg_link(&cache, &link), + } + }; + + match response { + Ok(resp) => resp, + Err(err) => { + println!("something failed: {}", err); + rouille::Response::text(format!( + r#"ugh, something broke: {} nobody has been alerted about this and it has probably not been -logged. pm me on the forums if you think it's important enough."#, - err - )) - } - } +logged. pm me on the forums if you think it's important."#, + err + )) } } }); -- cgit 1.4.1