-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add WasmError Enables idiomatic Javascript error handling with js_sys::Error which includes error enum variant names. * Add WasmError test
- Loading branch information
Showing
41 changed files
with
298 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2020-2021 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::borrow::Cow; | ||
|
||
use wasm_bindgen::JsValue; | ||
|
||
/// Convert an error into an idiomatic [js_sys::Error]. | ||
pub fn wasm_error<'a, T>(error: T) -> JsValue | ||
where | ||
T: Into<WasmError<'a>>, | ||
{ | ||
let wasm_err: WasmError = error.into(); | ||
JsValue::from(wasm_err) | ||
} | ||
|
||
/// Convenience struct to convert internal errors to [js_sys::Error]. Uses [std::borrow::Cow] | ||
/// internally to avoid unnecessary clones. | ||
/// | ||
/// This is a workaround for orphan rules so we can implement [core::convert::From] on errors from | ||
/// dependencies. | ||
#[derive(Debug, Clone)] | ||
pub struct WasmError<'a> { | ||
pub name: Cow<'a, str>, | ||
pub message: Cow<'a, str>, | ||
} | ||
|
||
impl<'a> WasmError<'a> { | ||
pub fn new(name: Cow<'a, str>, message: Cow<'a, str>) -> Self { | ||
Self { name, message } | ||
} | ||
} | ||
|
||
/// Convert [WasmError] into [js_sys::Error] for idiomatic error handling. | ||
impl From<WasmError<'_>> for js_sys::Error { | ||
fn from(error: WasmError<'_>) -> Self { | ||
let js_error = js_sys::Error::new(&error.message); | ||
js_error.set_name(&error.name); | ||
js_error | ||
} | ||
} | ||
|
||
/// Convert [WasmError] into [wasm_bindgen::JsValue]. | ||
impl From<WasmError<'_>> for JsValue { | ||
fn from(error: WasmError<'_>) -> Self { | ||
JsValue::from(js_sys::Error::from(error)) | ||
} | ||
} | ||
|
||
/// Implement WasmError for each type individually rather than a trait due to Rust's orphan rules. | ||
/// Each type must implement `Into<&'static str> + Display`. The `Into<&'static str>` trait can be | ||
/// derived using `strum::IntoStaticStr`. | ||
#[macro_export] | ||
macro_rules! impl_wasm_error_from { | ||
( $($t:ty),* ) => { | ||
$(impl From<$t> for WasmError<'_> { | ||
fn from(error: $t) -> Self { | ||
Self { | ||
message: Cow::Owned(error.to_string()), | ||
name: Cow::Borrowed(error.into()), | ||
} | ||
} | ||
})* | ||
} | ||
} | ||
|
||
impl_wasm_error_from!( | ||
identity::comm::Error, | ||
identity::core::Error, | ||
identity::credential::Error, | ||
identity::did::Error, | ||
identity::iota::Error | ||
); | ||
|
||
impl From<serde_json::Error> for WasmError<'_> { | ||
fn from(error: serde_json::Error) -> Self { | ||
Self { | ||
name: Cow::Borrowed("serde_json::Error"), // the exact error code is embedded in the message | ||
message: Cow::Owned(error.to_string()), | ||
} | ||
} | ||
} | ||
|
||
impl From<identity::iota::BeeMessageError> for WasmError<'_> { | ||
fn from(error: identity::iota::BeeMessageError) -> Self { | ||
Self { | ||
name: Cow::Borrowed("bee_message::Error"), | ||
message: Cow::Owned(error.to_string()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.