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,8 +232,9 @@ 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| {
let response = {
match TgLink::parse(request.raw_url()) { match TgLink::parse(request.raw_url()) {
None => rouille::Response::text( None => Ok(rouille::Response::text(
r#"tgsa r#"tgsa
---- ----
@ -255,23 +256,23 @@ 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),
match response {
Ok(resp) => resp,
Err(err) => { Err(err) => {
println!("something failed: {}", err); println!("something failed: {}", err);
rouille::Response::text(format!( rouille::Response::text(format!(
r#"something broke: {} 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
)) ))
} }
} }
}
}
}); });
} }