make internal_err an extension trait

This commit is contained in:
Peter Cai 2020-04-07 19:40:22 +08:00
parent 5467fa6e6d
commit 0547cdd0c3
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
3 changed files with 26 additions and 29 deletions

View File

@ -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)
)
)
).internal_err()
}
#[wasm_bindgen]

View File

@ -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,11 +47,8 @@ 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)
)?),
Some(&serde_json::to_string(&info).internal_err()?),
ResponseInit::new()
.status(200)
.headers({
@ -60,8 +57,7 @@ async fn get_actions(_req: Request, url: Url) -> MyResult<Response> {
cors!(headers);
headers
}.as_ref())
)
)
).internal_err()
}
pub enum Verb {

View File

@ -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())
}
}