make internal_err an extension trait
parent
5467fa6e6d
commit
0547cdd0c3
12
src/lib.rs
12
src/lib.rs
|
@ -7,7 +7,7 @@ mod router;
|
|||
mod sn;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
use utils::{Error, MyResult};
|
||||
use utils::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::*;
|
||||
|
||||
|
@ -43,12 +43,10 @@ async fn default_route(_req: Request, _url: Url) -> MyResult<Response> {
|
|||
}
|
||||
|
||||
async fn hello_world(_req: Request, _url: Url) -> MyResult<Response> {
|
||||
internal_err!(
|
||||
Response::new_with_opt_str_and_init(
|
||||
Some("Hello, world from Rust"),
|
||||
ResponseInit::new().status(200)
|
||||
)
|
||||
)
|
||||
Response::new_with_opt_str_and_init(
|
||||
Some("Hello, world from Rust"),
|
||||
ResponseInit::new().status(200)
|
||||
).internal_err()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
|
28
src/sn.rs
28
src/sn.rs
|
@ -1,7 +1,7 @@
|
|||
// Interface for Standard Notes (Actions)
|
||||
use crate::CONFIG;
|
||||
use crate::router::Router;
|
||||
use crate::utils::{Error, MyResult};
|
||||
use crate::utils::*;
|
||||
use serde::{Serialize, Serializer};
|
||||
use std::vec::Vec;
|
||||
use web_sys::*;
|
||||
|
@ -47,21 +47,17 @@ async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
|
|||
actions
|
||||
};
|
||||
|
||||
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())
|
||||
)
|
||||
)
|
||||
Response::new_with_opt_str_and_init(
|
||||
Some(&serde_json::to_string(&info).internal_err()?),
|
||||
ResponseInit::new()
|
||||
.status(200)
|
||||
.headers({
|
||||
let headers = Headers::new().unwrap();
|
||||
headers.set("Content-Type", "application/json").unwrap();
|
||||
cors!(headers);
|
||||
headers
|
||||
}.as_ref())
|
||||
).internal_err()
|
||||
}
|
||||
|
||||
pub enum Verb {
|
||||
|
|
15
src/utils.rs
15
src/utils.rs
|
@ -25,12 +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 trait ResultExt<T, E> {
|
||||
// Ignore any error and return InternalError for them all
|
||||
// Used in place of ugly `.unwrap()`.
|
||||
fn internal_err(self) -> Result<T, Error>;
|
||||
}
|
||||
|
||||
impl <T, E> ResultExt<T, E> for Result<T, E> {
|
||||
fn internal_err(self) -> Result<T, Error> {
|
||||
self.map_err(|_| crate::utils::Error::InternalError())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue