Skip to content

Commit 89b9d9d

Browse files
committed
Improve error handling for the wasm sdk's Cookie builder
1 parent b2da4a2 commit 89b9d9d

3 files changed

Lines changed: 20 additions & 33 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sdk/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ web = [
1414
"dep:gloo-console",
1515
"dep:gloo-net",
1616
"dep:gloo-storage",
17+
"dep:gloo-utils",
1718
"dep:js-sys",
1819
"dep:tokio-tungstenite-wasm",
1920
"dep:wasm-bindgen",
@@ -45,13 +46,12 @@ getrandom = { version = "0.3.2", features = ["wasm_js"], optional = true }
4546
gloo-console = { version = "0.3.0", optional = true }
4647
gloo-net = { version = "0.6.0", optional = true }
4748
gloo-storage = { version = "0.3.0", optional = true }
49+
gloo-utils = { version = "0.2.0", optional = true }
4850
js-sys = { version = "0.3", optional = true }
4951
tokio-tungstenite-wasm = { version = "0.6.0", optional = true }
5052
wasm-bindgen = { version = "0.2.100", optional = true }
5153
wasm-bindgen-futures = { version = "0.4.45", optional = true }
52-
web-sys = { version = "0.3.77", features = [
53-
"Document", "HtmlDocument", "Window"
54-
], optional = true}
54+
web-sys = { version = "0.3.77", features = ["HtmlDocument"], optional = true }
5555

5656
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
5757
home.workspace = true

crates/sdk/src/credentials.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,18 @@ mod web_mod {
160160
pub mod cookies {
161161
use thiserror::Error;
162162
use wasm_bindgen::{JsCast, JsValue};
163-
use web_sys::{window, Document, HtmlDocument};
163+
use web_sys::HtmlDocument;
164164

165165
#[derive(Error, Debug)]
166166
pub enum CookieError {
167-
#[error("Window Object not valid in this context")]
168-
NoWindow,
169-
#[error("No `document` available on `window` object")]
170-
NoDocument,
171-
#[error("`document` is not an HtmlDocument")]
172-
NoHtmlDocument,
173-
#[error("web_sys error: {0:?}")]
174-
WebSys(JsValue),
175-
}
167+
#[error("Error reading cookies: {0:?}")]
168+
Get(JsValue),
176169

177-
impl From<JsValue> for CookieError {
178-
fn from(err: JsValue) -> Self {
179-
CookieError::WebSys(err)
180-
}
170+
#[error("Error setting cookie `{key}`: {js_value:?}")]
171+
Set { key: String, js_value: JsValue },
181172
}
182173

183-
/// A builder for contructing and setting cookies.
174+
/// A builder for constructing and setting cookies.
184175
pub struct Cookie {
185176
name: String,
186177
value: String,
@@ -207,8 +198,8 @@ mod web_mod {
207198

208199
/// Gets the value of a cookie by name.
209200
pub fn get(name: &str) -> Result<Option<String>, CookieError> {
210-
let doc = get_html_document()?;
211-
let all = doc.cookie().map_err(CookieError::from)?;
201+
let doc = get_html_document();
202+
let all = doc.cookie().map_err(|e| CookieError::Get(e))?;
212203
for cookie in all.split(';') {
213204
let cookie = cookie.trim();
214205
if let Some((k, v)) = cookie.split_once('=') {
@@ -240,7 +231,7 @@ mod web_mod {
240231
}
241232

242233
/// Toggles the `Secure` flag.
243-
/// The default is `false`.
234+
/// Defaults to `false`.
244235
pub fn secure(mut self, enabled: bool) -> Self {
245236
self.secure = enabled;
246237
self
@@ -253,7 +244,7 @@ mod web_mod {
253244
}
254245

255246
pub fn set(self) -> Result<(), CookieError> {
256-
let doc = get_html_document()?;
247+
let doc = get_html_document();
257248
let mut parts = vec![format!("{}={}", self.name, self.value)];
258249

259250
if let Some(path) = self.path {
@@ -273,7 +264,10 @@ mod web_mod {
273264
}
274265

275266
let cookie_str = parts.join("; ");
276-
doc.set_cookie(&cookie_str).map_err(CookieError::from)
267+
doc.set_cookie(&cookie_str).map_err(|e| CookieError::Set {
268+
key: self.name.clone(),
269+
js_value: e,
270+
})
277271
}
278272

279273
/// Deletes the cookie by setting its value to empty and `Max-Age=0`.
@@ -305,16 +299,8 @@ mod web_mod {
305299
}
306300
}
307301

308-
fn get_document() -> Result<Document, CookieError> {
309-
window()
310-
.ok_or(CookieError::NoWindow)?
311-
.document()
312-
.ok_or(CookieError::NoDocument)
313-
}
314-
315-
fn get_html_document() -> Result<HtmlDocument, CookieError> {
316-
let doc = get_document()?;
317-
doc.dyn_into::<HtmlDocument>().map_err(|_| CookieError::NoHtmlDocument)
302+
fn get_html_document() -> HtmlDocument {
303+
gloo_utils::document().unchecked_into::<HtmlDocument>()
318304
}
319305
}
320306
}

0 commit comments

Comments
 (0)