mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-23 19:00:13 +02:00
19 lines
368 B
Markdown
19 lines
368 B
Markdown
|
# Error handling for imported Javascript methods
|
||
|
|
||
|
Javascript methods that throw can be imported in Rust like this:
|
||
|
|
||
|
```javascript
|
||
|
window.throwsIfPair = (num) => {
|
||
|
if (num % 2 === 0) throw new Error("Pair number");
|
||
|
return num;
|
||
|
};
|
||
|
```
|
||
|
|
||
|
```rust
|
||
|
#[wasm_bindgen]
|
||
|
extern "C" {
|
||
|
#[wasm_bindgen(catch)]
|
||
|
pub fn throwsIfPair(text: i32) -> Result<i32, JsValue>;
|
||
|
}
|
||
|
```
|