Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Move paste error projection to WASM binding

Manav Rathiea8f371f9169
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 01353cb100..f3221eacf8 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2678,9 +2678,11 @@ dependencies = [
name = "ente-paste-wasm"
version = "0.0.0"
dependencies = [
+ "ente-core",
"ente-paste",
"ente-wasm-log",
"js-sys",
+ "thiserror 2.0.18",
"wasm-bindgen",
"wasm-bindgen-futures",
]
diff --git a/rust/bindings/wasm/paste/Cargo.toml b/rust/bindings/wasm/paste/Cargo.toml
index 18813559ae..f1e9d455c8 100644
--- a/rust/bindings/wasm/paste/Cargo.toml
+++ b/rust/bindings/wasm/paste/Cargo.toml
@@ -6,8 +6,10 @@ edition = "2024"
crate-type = ["cdylib"]
[dependencies]
+ente-core.workspace = true
ente-wasm-log.workspace = true
ente-paste.workspace = true
js-sys.workspace = true
+thiserror.workspace = true
wasm-bindgen.workspace = true
wasm-bindgen-futures.workspace = true
diff --git a/rust/bindings/wasm/paste/src/lib.rs b/rust/bindings/wasm/paste/src/lib.rs
index 1ff9c83ff6..7f4444d256 100644
--- a/rust/bindings/wasm/paste/src/lib.rs
+++ b/rust/bindings/wasm/paste/src/lib.rs
@@ -2,18 +2,48 @@ use ente_paste::{Client, OpenPaste, PasteSession};
use ente_wasm_log as _;
use wasm_bindgen::prelude::*;
-pub struct PasteError(ente_paste::Error);
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ #[error(transparent)]
+ Paste(#[from] ente_paste::Error),
+}
+
+impl Error {
+ fn name(&self) -> Option<&'static str> {
+ use ente_paste::Error as E;
+
+ match self {
+ Self::Paste(E::Http(ente_core::http::Error::Network(_))) => Some("network"),
+ Self::Paste(E::Http(_)) => Some("request_failed"),
+ Self::Paste(E::Crypto(_)) => Some("crypto"),
+ Self::Paste(E::Base64Decode(_)) | Self::Paste(E::MalformedPayload) => {
+ Some("malformed_payload")
+ }
+ Self::Paste(E::IncorrectPassword) => Some("incorrect_password"),
+ Self::Paste(E::Unavailable) => Some("unavailable"),
+ Self::Paste(E::EmptyText) => Some("empty_text"),
+ Self::Paste(E::TextTooLong) => Some("text_too_long"),
+ Self::Paste(E::InvalidLink) => Some("invalid_link"),
+ Self::Paste(E::InvalidAccessToken) => Some("invalid_access_token"),
+ Self::Paste(E::InvalidKey) => Some("invalid_key"),
+ Self::Paste(E::MissingKey) => Some("missing_key"),
+ Self::Paste(E::KeyMismatch) => Some("key_mismatch"),
+ Self::Paste(E::PasswordRequired) => Some("password_required"),
+ _ => None,
+ }
+ }
-impl From<ente_paste::Error> for PasteError {
- fn from(error: ente_paste::Error) -> Self {
- Self(error)
+ fn message(&self) -> String {
+ ente_core::error::chain(self)
}
}
-impl From<PasteError> for JsValue {
- fn from(error: PasteError) -> Self {
- let js_error = js_sys::Error::new(&error.0.to_string());
- js_error.set_name(error.0.code());
+impl From<Error> for JsValue {
+ fn from(error: Error) -> Self {
+ let js_error = js_sys::Error::new(&error.message());
+ if let Some(name) = error.name() {
+ js_error.set_name(name);
+ }
js_error.into()
}
}
@@ -65,7 +95,7 @@ pub struct PasteClient {
#[wasm_bindgen]
impl PasteClient {
#[wasm_bindgen(constructor)]
- pub fn new(api_origin: String) -> Result<Self, PasteError> {
+ pub fn new(api_origin: String) -> Result<Self, Error> {
Ok(Self {
client: Client::new(api_origin, None)?,
session: None,
@@ -77,7 +107,7 @@ impl PasteClient {
paste_origin: &str,
text: &str,
password: Option<String>,
- ) -> Result<CreatedPaste, PasteError> {
+ ) -> Result<CreatedPaste, Error> {
let link = self.client.create(text, password.as_deref()).await?;
Ok(CreatedPaste {
url: link.url(paste_origin),
@@ -85,7 +115,7 @@ impl PasteClient {
})
}
- pub async fn open(&mut self, url: &str) -> Result<OpenedPaste, PasteError> {
+ pub async fn open(&mut self, url: &str) -> Result<OpenedPaste, Error> {
let mut session = PasteSession::parse(url)?;
let opened = match session.open(&self.client).await? {
OpenPaste::PasswordRequired => OpenedPaste {
@@ -102,7 +132,7 @@ impl PasteClient {
}
#[wasm_bindgen(js_name = submitPassword)]
- pub async fn submit_password(&mut self, password: &str) -> Result<String, PasteError> {
+ pub async fn submit_password(&mut self, password: &str) -> Result<String, Error> {
self.session
.as_mut()
.ok_or(ente_paste::Error::SessionNotOpen)?
diff --git a/rust/crates/paste/src/lib.rs b/rust/crates/paste/src/lib.rs
index 862c720257..a14478167a 100644
--- a/rust/crates/paste/src/lib.rs
+++ b/rust/crates/paste/src/lib.rs
@@ -66,29 +66,6 @@ pub enum Error {
pub type Result<T> = std::result::Result<T, Error>;
-impl Error {
- pub fn code(&self) -> &'static str {
- match self {
- Self::Http(http::Error::Network(_)) => "network",
- Self::Http(_) => "request_failed",
- Self::Crypto(_) => "crypto",
- Self::Base64Decode(_) | Self::MalformedPayload => "malformed_payload",
- Self::IncorrectPassword => "incorrect_password",
- Self::Unavailable => "unavailable",
- Self::EmptyText => "empty_text",
- Self::TextTooLong => "text_too_long",
- Self::InvalidLink => "invalid_link",
- Self::InvalidAccessToken => "invalid_access_token",
- Self::InvalidKey => "invalid_key",
- Self::MissingKey => "missing_key",
- Self::KeyMismatch => "key_mismatch",
- Self::PasswordRequired => "password_required",
- Self::SessionNotOpen => "session_not_open",
- Self::MissingGuardCookie => "missing_guard_cookie",
- }
- }
-}
-
#[derive(Debug, Eq, PartialEq)]
pub struct PasteKey {
fragment_secret: String,