about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-05-14T23·04+0200
committertazjin <tazjin@tvl.su>2022-05-14T23·50+0000
commitf200b1265f62a1d4c91dbeeb7100dd79afb261fa (patch)
tree95626815c72c8107a7897cc0b48c265aedd81812
parente3d09c3446b6a5ba7e7fafa66712b3420714e226 (diff)
refactor(tazjin/tgsa): move error handling one layer up r/4076
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>
-rw-r--r--users/tazjin/tgsa/src/main.rs39
1 files changed, 20 insertions, 19 deletions
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<TgPost> {
     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)?;
-    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
+                ))
             }
         }
     });