Skip to content

Commit c0f2c6d

Browse files
authored
Update to Rust 2021 and use JsError from gloo-utils (#10)
* Update to Rust 2021 and use JsError from gloo-utils * use new toolchain
1 parent fe1036f commit c0f2c6d

File tree

6 files changed

+24
-30
lines changed

6 files changed

+24
-30
lines changed

.github/workflows/ci.yml

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ jobs:
1616
steps:
1717
- uses: actions/checkout@v2
1818

19+
- uses: actions-rs/toolchain@v1
20+
with:
21+
toolchain: stable
22+
profile: minimal
23+
components: clippy
24+
target: wasm32-unknown-unknown
25+
1926
- uses: actions/cache@v2
2027
with:
2128
path: |
@@ -77,6 +84,13 @@ jobs:
7784

7885
steps:
7986
- uses: actions/checkout@v2
87+
- uses: actions-rs/toolchain@v1
88+
with:
89+
toolchain: stable
90+
profile: minimal
91+
components: clippy
92+
target: wasm32-unknown-unknown
93+
8094

8195
- name: Install wasm-pack
8296
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "reqwasm"
33
version = "0.2.1"
44
authors = ["Hamza <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66
license = "MIT OR Apache-2.0"
77
repository = "https://github.com/hamza1311/reqwasm"
88
description = "HTTP requests library for WASM Apps"
@@ -19,9 +19,9 @@ js-sys = "0.3"
1919
wasm-bindgen-futures = "0.4"
2020
serde = { version = "1.0", features = ["derive"] }
2121
serde_json = "1.0"
22-
anyhow = "1.0"
2322
thiserror = "1.0"
2423
futures = "0.3.14"
24+
gloo-utils = "0.1.0"
2525

2626
async-broadcast = "0.3"
2727
pin-project = "1"

src/error.rs

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
use std::fmt;
1+
use gloo_utils::errors::JsError;
22
use thiserror::Error as ThisError;
3-
use wasm_bindgen::{JsCast, JsValue};
4-
5-
#[derive(Debug, Clone)]
6-
#[doc(hidden)]
7-
pub struct JsError {
8-
pub name: String,
9-
pub message: String,
10-
js_to_string: String,
11-
}
12-
13-
impl fmt::Display for JsError {
14-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15-
write!(f, "{}", self.js_to_string)
16-
}
17-
}
3+
use wasm_bindgen::JsValue;
184

195
/// All the errors returned by this crate.
206
#[derive(Debug, ThisError)]
@@ -29,22 +15,15 @@ pub enum Error {
2915
#[from]
3016
serde_json::Error,
3117
),
32-
/// Unknown error.
33-
#[error("{0}")]
34-
Other(anyhow::Error),
3518
}
3619

3720
pub(crate) fn js_to_error(js_value: JsValue) -> Error {
3821
Error::JsError(js_to_js_error(js_value))
3922
}
4023

4124
pub(crate) fn js_to_js_error(js_value: JsValue) -> JsError {
42-
match js_value.dyn_into::<js_sys::Error>() {
43-
Ok(error) => JsError {
44-
name: String::from(error.name()),
45-
message: String::from(error.message()),
46-
js_to_string: String::from(error.to_string()),
47-
},
25+
match JsError::try_from(js_value) {
26+
Ok(error) => error,
4827
Err(_) => unreachable!("JsValue passed is not an Error type -- this is a bug"),
4928
}
5029
}

src/http.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl Request {
163163
Ok(response) => Ok(Response {
164164
response: response.unchecked_into(),
165165
}),
166-
Err(_) => Err(Error::Other(anyhow::anyhow!("can't convert to Response"))),
166+
Err(e) => panic!("fetch returned {:?}, not `Response` - this is a bug", e),
167167
}
168168
}
169169

src/websocket/futures.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
//! })
3030
//! # }
3131
//! ```
32+
use crate::js_to_js_error;
3233
use crate::websocket::{
3334
events::{CloseEvent, ErrorEvent},
3435
Message, State, WebSocketError,
3536
};
36-
use crate::{js_to_js_error, JsError};
3737
use async_broadcast::Receiver;
3838
use futures::ready;
3939
use futures::{Sink, Stream};
40+
use gloo_utils::errors::JsError;
4041
use pin_project::{pin_project, pinned_drop};
4142
use std::cell::RefCell;
4243
use std::pin::Pin;

src/websocket/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
pub mod events;
77
pub mod futures;
88

9-
use crate::JsError;
109
use events::{CloseEvent, ErrorEvent};
10+
use gloo_utils::errors::JsError;
1111
use std::fmt;
1212

1313
/// Message sent to and received from WebSocket.

0 commit comments

Comments
 (0)