Skip to content

Commit

Permalink
Improve error messages by moving unwrap => expect
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Oct 20, 2022
1 parent a463bcc commit 0e1f3c5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
3 changes: 2 additions & 1 deletion leptos_reactive/src/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ where
pub fn with<U>(&self, f: impl Fn(&T) -> U) -> U {
// okay to unwrap here, because the value will *always* have initially
// been set by the effect, synchronously
self.0.with(|n| f(n.as_ref().unwrap()))
self.0
.with(|n| f(n.as_ref().expect("Memo is missing its initial value")))
}
}

Expand Down
20 changes: 10 additions & 10 deletions leptos_reactive/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ where
if let Some(data) = context.resolved_resources.remove(&id) {
context.pending_resources.remove(&id); // no longer pending
r.resolved.set(true);
//let decoded = base64::decode(&data).unwrap_throw();
//let res = bincode::deserialize(&decoded).unwrap_throw();
let res = serde_json::from_str(&data).unwrap_throw();
let res =
serde_json::from_str(&data).expect_throw("could not deserialize Resource JSON");
r.set_value.update(|n| *n = Some(res));
r.set_loading.update(|n| *n = false);

Expand All @@ -163,9 +162,8 @@ where
let set_value = r.set_value;
let set_loading = r.set_loading;
move |res: String| {
//let decoded = base64::decode(&res).unwrap_throw();
//let res = bincode::deserialize(&decoded).unwrap_throw();
let res = serde_json::from_str(&res).unwrap_throw();
let res = serde_json::from_str(&res)
.expect_throw("could not deserialize JSON for already-resolved Resource");
resolved.set(true);
set_value.update(|n| *n = res);
set_loading.update(|n| *n = false);
Expand All @@ -177,8 +175,8 @@ where
&web_sys::window().unwrap(),
&wasm_bindgen::JsValue::from_str("__LEPTOS_RESOURCE_RESOLVERS"),
)
.unwrap();
let id = serde_json::to_string(&id).unwrap();
.expect_throw("no __LEPTOS_RESOURCE_RESOLVERS found in the JS global scope");
let id = serde_json::to_string(&id).expect_throw("could not deserialize Resource ID");
_ = js_sys::Reflect::set(
&resource_resolvers,
&wasm_bindgen::JsValue::from_str(&id),
Expand Down Expand Up @@ -418,8 +416,10 @@ where
let fut = (self.fetcher)(self.source.get());
Box::pin(async move {
let res = fut.await;
(id, serde_json::to_string(&res).unwrap())
//(id, base64::encode(&bincode::serialize(&res).unwrap()))
(
id,
serde_json::to_string(&res).expect("could not serialize Resource"),
)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion leptos_reactive/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Runtime {
for i in 0..templates.length() {
let node = templates
.item(i)
.unwrap_throw()
.unwrap_throw() // ok to unwrap; we already have the index, so this can't fail
.unchecked_into::<web_sys::Element>();
let key = node.get_attribute("data-hk").unwrap_throw();
registry.insert(key, node);
Expand Down
12 changes: 8 additions & 4 deletions leptos_reactive/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ impl Scope {
pub fn child_scope(self, f: impl FnOnce(Scope)) -> ScopeDisposer {
let (_, child_id, disposer) = self.runtime.run_scope_undisposed(f, Some(self));
let mut children = self.runtime.scope_children.borrow_mut();
children.entry(self.id).unwrap().or_default().push(child_id);
children
.entry(self.id)
.expect("trying to add a child to a Scope that has already been disposed")
.or_default()
.push(child_id);
disposer
}

Expand Down Expand Up @@ -145,7 +149,7 @@ pub fn on_cleanup(cx: Scope, cleanup_fn: impl FnOnce() + 'static) {
let mut cleanups = cx.runtime.scope_cleanups.borrow_mut();
let cleanups = cleanups
.entry(cx.id)
.unwrap()
.expect("trying to clean up a Scope that has already been disposed")
.or_insert_with(Default::default);
cleanups.push(Box::new(cleanup_fn));
}
Expand Down Expand Up @@ -193,10 +197,10 @@ impl Scope {
.unchecked_ref::<web_sys::HtmlTemplateElement>()
.content()
.clone_node_with_deep(true)
.unwrap_throw()
.expect_throw("(get_next_element) could not clone template")
.unchecked_into::<web_sys::Element>()
.first_element_child()
.unwrap_throw();
.expect_throw("(get_next_element) could not get first child of template");
t
};

Expand Down
10 changes: 6 additions & 4 deletions router/src/data/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ where
.downcast_ref::<T>()
.cloned()
.unwrap_or_else(|| {
debug_warn!(
panic!(
"use_loader() could not downcast to {:?}",
std::any::type_name::<T>(),
);
panic!()
)
})
}
},
Expand Down Expand Up @@ -81,7 +80,10 @@ where
cx,
move || (params.get(), url()),
move |(params, url)| async move {
log::debug!("[LOADER] calling loader; should fire whenever params or URL change");
log::debug!(
"[LOADER] calling loader with {:#?}; should fire whenever params or URL change",
(params, url)
);

let route = use_route(cx);
let query = use_query_map(cx);
Expand Down

0 comments on commit 0e1f3c5

Please sign in to comment.