refactor(tazjin/tgsa): move error handling one layer up

this lays the groundwork for adding another handler and returning
handler results as `anyhow::Result<rouille::Response>`.

needed for the image redirect stuff.

Change-Id: I909bd9c2f46f42ea759d50662d7bc36c1f408ed3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5609
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Vincent Ambo 2022-05-15 01:04:55 +02:00 committed by tazjin
parent e3d09c3446
commit f200b1265f

View file

@ -221,9 +221,9 @@ fn fetch_with_cache(cache: &Cache, link: &TgLink) -> Result<TgPost> {
Ok(post) Ok(post)
} }
fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result<String> { fn handle_tg_link(cache: &Cache, link: &TgLink) -> Result<rouille::Response> {
let post = fetch_with_cache(cache, link)?; let post = fetch_with_cache(cache, link)?;
Ok(post.bbcode) Ok(rouille::Response::text(post.bbcode))
} }
fn main() { fn main() {
@ -232,9 +232,10 @@ fn main() {
let cache: Cache = RwLock::new(HashMap::new()); let cache: Cache = RwLock::new(HashMap::new());
rouille::start_server("0.0.0.0:8472", move |request| { rouille::start_server("0.0.0.0:8472", move |request| {
match TgLink::parse(request.raw_url()) { let response = {
None => rouille::Response::text( match TgLink::parse(request.raw_url()) {
r#"tgsa None => Ok(rouille::Response::text(
r#"tgsa
---- ----
this is a stupid program that lets you turn telegram message links 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. pm me on the forums if this makes you mad or something.
"#, "#,
), )),
Some(link) => { Some(link) => handle_tg_link(&cache, &link),
let result = handle_tg_link(&cache, &link); }
match result { };
Ok(bbcode) => rouille::Response::text(bbcode),
Err(err) => { match response {
println!("something failed: {}", err); Ok(resp) => resp,
rouille::Response::text(format!( Err(err) => {
r#"something broke: {} println!("something failed: {}", err);
rouille::Response::text(format!(
r#"ugh, something broke: {}
nobody has been alerted about this and it has probably not been 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."#, logged. pm me on the forums if you think it's important."#,
err err
)) ))
}
}
} }
} }
}); });