add internal_err! macro to replace some unwrap()
parent
a2fa677514
commit
5467fa6e6d
10
src/lib.rs
10
src/lib.rs
|
@ -43,10 +43,12 @@ async fn default_route(_req: Request, _url: Url) -> MyResult<Response> {
|
|||
}
|
||||
|
||||
async fn hello_world(_req: Request, _url: Url) -> MyResult<Response> {
|
||||
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]
|
||||
|
|
27
src/sn.rs
27
src/sn.rs
|
@ -47,18 +47,21 @@ async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
|
|||
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 {
|
||||
|
|
|
@ -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<T> = Result<T, Error>;
|
||||
|
||||
pub enum Error {
|
||||
|
|
Loading…
Reference in New Issue