improve error handling

This commit is contained in:
Jonas Kruckenberg 2022-11-13 21:06:06 +01:00
parent c9fa93de72
commit 355febf927
12 changed files with 443 additions and 258 deletions

View file

@ -1,6 +1,8 @@
use serde::{de::DeserializeOwned, Serialize};
use url::Url;
use crate::Error;
/// Convert a device file path to an URL that can be loaded by the webview.
/// Note that `asset:` and `https://asset.localhost` must be added to [`tauri.security.csp`](https://tauri.app/v1/api/config/#securityconfig.csp) in `tauri.conf.json`.
/// Example CSP value: `"csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost"` to use the asset protocol on image sources.
@ -36,14 +38,12 @@ use url::Url;
///
/// @return the URL that can be used as source on the webview.
#[inline(always)]
pub async fn convert_file_src(file_path: &str, protocol: Option<&str>) -> Url {
Url::parse(
&inner::convertFileSrc(file_path, protocol)
.await
.as_string()
.unwrap(),
)
.unwrap()
pub async fn convert_file_src(file_path: &str, protocol: Option<&str>) -> crate::Result<Url> {
let js_val = inner::convertFileSrc(file_path, protocol)
.await
.map_err(Error::Other)?;
Ok(serde_wasm_bindgen::from_value(js_val)?)
}
/// Sends a message to the backend.
@ -66,9 +66,10 @@ pub async fn convert_file_src(file_path: &str, protocol: Option<&str>) -> Url {
/// @return A promise resolving or rejecting to the backend response.
#[inline(always)]
pub async fn invoke<A: Serialize, R: DeserializeOwned>(cmd: &str, args: &A) -> crate::Result<R> {
let res = inner::invoke(cmd, serde_wasm_bindgen::to_value(args).unwrap()).await;
let raw = inner::invoke(cmd, serde_wasm_bindgen::to_value(args)?)
.await
.map_err(crate::Error::Other)?;
let raw = res.map_err(crate::Error::Other)?;
serde_wasm_bindgen::from_value(raw).map_err(Into::into)
}
@ -77,14 +78,18 @@ pub async fn invoke<A: Serialize, R: DeserializeOwned>(cmd: &str, args: &A) -> c
///
/// @return A unique identifier associated with the callback function.
#[inline(always)]
pub async fn transform_callback<T: DeserializeOwned>(callback: &dyn Fn(T), once: bool) -> f64 {
inner::transformCallback(
pub async fn transform_callback<T: DeserializeOwned>(
callback: &dyn Fn(T),
once: bool,
) -> crate::Result<f64> {
let js_val = inner::transformCallback(
&|raw| callback(serde_wasm_bindgen::from_value(raw).unwrap()),
once,
)
.await
.as_f64()
.unwrap()
.map_err(Error::Other)?;
Ok(serde_wasm_bindgen::from_value(js_val)?)
}
mod inner {
@ -92,9 +97,17 @@ mod inner {
#[wasm_bindgen(module = "/dist/tauri.js")]
extern "C" {
pub async fn convertFileSrc(filePath: &str, protocol: Option<&str>) -> JsValue;
#[wasm_bindgen(catch)]
pub async fn convertFileSrc(
filePath: &str,
protocol: Option<&str>,
) -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
pub async fn invoke(cmd: &str, args: JsValue) -> Result<JsValue, JsValue>;
pub async fn transformCallback(callback: &dyn Fn(JsValue), once: bool) -> JsValue;
#[wasm_bindgen(catch)]
pub async fn transformCallback(
callback: &dyn Fn(JsValue),
once: bool,
) -> Result<JsValue, JsValue>;
}
}