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,3 +1,5 @@
use crate::Error;
/// Gets the clipboard content as plain text.
///
/// # Example
@ -8,8 +10,10 @@
/// let clipboard_text = read_text().await;
/// ```
#[inline(always)]
pub async fn read_text() -> Option<String> {
inner::readText().await.as_string()
pub async fn read_text() -> crate::Result<String> {
let js_val = inner::readText().await.map_err(Error::Other)?;
Ok(serde_wasm_bindgen::from_value(js_val)?)
}
/// Writes plain text to the clipboard.
@ -25,8 +29,8 @@ pub async fn read_text() -> Option<String> {
///
/// @returns A promise indicating the success or failure of the operation.
#[inline(always)]
pub async fn write_text(text: &str) {
inner::writeText(text).await
pub async fn write_text(text: &str) -> crate::Result<()> {
inner::writeText(text).await.map_err(Error::Other)
}
mod inner {
@ -34,7 +38,9 @@ mod inner {
#[wasm_bindgen(module = "/dist/clipboard.js")]
extern "C" {
pub async fn readText() -> JsValue;
pub async fn writeText(text: &str);
#[wasm_bindgen(catch)]
pub async fn readText() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
pub async fn writeText(text: &str) -> Result<(), JsValue>;
}
}