diff --git a/src/lib.rs b/src/lib.rs index 483125c..1366427 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,10 +43,12 @@ async fn default_route(_req: Request, _url: Url) -> MyResult { } async fn hello_world(_req: Request, _url: Url) -> MyResult { - Ok(Response::new_with_opt_str_and_init( - Some("Hello, world from Rust"), - ResponseInit::new().status(200) - ).unwrap()) + internal_err!( + Response::new_with_opt_str_and_init( + Some("Hello, world from Rust"), + ResponseInit::new().status(200) + ) + ) } #[wasm_bindgen] diff --git a/src/sn.rs b/src/sn.rs index 03cc476..c82f609 100644 --- a/src/sn.rs +++ b/src/sn.rs @@ -47,18 +47,21 @@ async fn get_actions(_req: Request, url: Url) -> MyResult { actions }; - Ok(Response::new_with_opt_str_and_init( - Some(&serde_json::to_string(&info) - .map_err(|_| Error::InternalError())?), - ResponseInit::new() - .status(200) - .headers({ - let headers = Headers::new().unwrap(); - headers.set("Content-Type", "application/json").unwrap(); - cors!(headers); - headers - }.as_ref()) - ).unwrap()) + internal_err!( + Response::new_with_opt_str_and_init( + Some(&internal_err!( + serde_json::to_string(&info) + )?), + ResponseInit::new() + .status(200) + .headers({ + let headers = Headers::new().unwrap(); + headers.set("Content-Type", "application/json").unwrap(); + cors!(headers); + headers + }.as_ref()) + ) + ) } pub enum Verb { diff --git a/src/utils.rs b/src/utils.rs index ed59f34..7009ddf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -25,6 +25,15 @@ macro_rules! cors { }; } +// Ignore any error and return InternalError for them all +// Used in place of ugly `.unwrap()`. +#[macro_export] +macro_rules! internal_err { + ($result:expr) => { + $result.map_err(|_| crate::utils::Error::InternalError()) + } +} + pub type MyResult = Result; pub enum Error {