Skip to content

Commit c728e77

Browse files
committed
future_to_promise: accept JsError instead of anyhow::Error
`future_to_promise` is responsible for taking the `Future`s that result from running rust code, and wrapping them into javascript Promises. Currently, it takes a `Result<T, anyhow::Error>`, meaning that all our errors get wrapped into `anyhow::Error`; `future_to_promise` then converts that `anyhow::Error` into a `JsError`. However: 1. This doesn't work terribly well under matrix-rust-sdk 0.12.0, because some of our error types no longer implement `Send + Sync`, which means that `anyhow::Error::from()` will no longer accept them. 2. It's redundant anyway. It turns out that almost all of the async blocks we pass to `future_to_promise` are just as happy to directly return a `JsError`, thus cutting out the middleman. And `JsError::from(std::error::Error)` doesn't require that the error be `Send + Sync`. The only remaining exception is in `verification.rs`, which is soon fixed up.
1 parent f2418ba commit c728e77

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/future.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
use std::future::Future;
22

3-
use futures_util::TryFutureExt;
43
use js_sys::Promise;
54
use wasm_bindgen::{JsError, JsValue, UnwrapThrowExt};
65
use wasm_bindgen_futures::spawn_local;
76

7+
/**
8+
* Convert a Rust [`Future`] which returns [`Result<T, JsError>`] into a
9+
* Javascript [`Promise`] which either resolves with an object of type `T`,
10+
* or rejects with an error of type [`Error`].
11+
*
12+
* [`Error`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
13+
*/
814
pub(crate) fn future_to_promise<F, T>(future: F) -> Promise
915
where
10-
F: Future<Output = Result<T, anyhow::Error>> + 'static,
16+
F: Future<Output = Result<T, JsError>> + 'static,
1117
T: Into<JsValue>,
1218
{
13-
future_to_promise_with_custom_error(future.map_err(|error| JsError::new(&error.to_string())))
19+
future_to_promise_with_custom_error(future)
1420
}
1521

22+
/**
23+
* Convert a Rust [`Future`] which returns [`Result<T, E>`] into a
24+
* Javascript [`Promise`] which either resolves with an object of type `T`,
25+
* or rejects with an error of type `E`.
26+
*/
1627
pub(crate) fn future_to_promise_with_custom_error<F, T, E>(future: F) -> Promise
1728
where
1829
F: Future<Output = Result<T, E>> + 'static,

src/verification.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,7 @@ impl VerificationRequest {
10341034
.transpose()
10351035
{
10361036
Ok(a) => Ok(a),
1037-
Err(_) => {
1038-
Err(anyhow::Error::msg("Failed to build the outgoing verification request"))
1039-
}
1037+
Err(_) => Err(JsError::new("Failed to build the outgoing verification request")),
10401038
}
10411039
})
10421040
}

0 commit comments

Comments
 (0)