-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for FlatError in wasm
- Loading branch information
Showing
7 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
/** | ||
* Error thrown by the WASM module. | ||
* @param {string} message - Error message. | ||
* @extends Error | ||
*/ | ||
class WasmError extends Error { | ||
constructor(message, name) { | ||
super(message); | ||
this.name = name ?? "WasmError"; | ||
} | ||
} | ||
|
||
exports.WasmError = WasmError; |
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 |
---|---|---|
@@ -1,28 +1,48 @@ | ||
use bitwarden_error::FlatError; | ||
use wasm_bindgen::prelude::*; | ||
|
||
// Importing an error class defined in JavaScript instead of defining it in Rust | ||
// allows us to extend the `Error` class. It also provides much better console output. | ||
#[wasm_bindgen] | ||
#[wasm_bindgen(module = "/src/error.js")] | ||
extern "C" { | ||
#[wasm_bindgen(js_name = Error)] | ||
type JsError; | ||
type WasmError; | ||
|
||
#[wasm_bindgen(constructor, js_class = Error)] | ||
fn new(message: String) -> JsError; | ||
#[wasm_bindgen(constructor)] | ||
fn new(message: String, name: Option<String>) -> WasmError; | ||
} | ||
|
||
pub type Result<T, E = GenericError> = std::result::Result<T, E>; | ||
|
||
pub struct GenericError(pub String); | ||
|
||
pub struct FlattenedError { | ||
pub variant: String, | ||
pub message: String, | ||
} | ||
|
||
impl<T: ToString> From<T> for GenericError { | ||
fn from(error: T) -> Self { | ||
GenericError(error.to_string()) | ||
} | ||
} | ||
|
||
impl<T: FlatError> From<T> for FlattenedError { | ||
fn from(error: T) -> Self { | ||
FlattenedError { | ||
variant: error.get_variant().to_owned(), | ||
message: error.get_message().to_owned(), | ||
} | ||
} | ||
} | ||
|
||
impl From<GenericError> for JsValue { | ||
fn from(error: GenericError) -> Self { | ||
JsError::new(error.0).into() | ||
WasmError::new(error.0, None).into() | ||
} | ||
} | ||
|
||
impl From<FlattenedError> for JsValue { | ||
fn from(error: FlattenedError) -> Self { | ||
WasmError::new(error.message, Some(error.variant)).into() | ||
} | ||
} |