bubble_error.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //! we should properly bubble up errors from components
  2. use std::{error::Error as StdError, marker::PhantomData, string::ParseError};
  3. use anyhow::{anyhow, bail};
  4. use dioxus::prelude::*;
  5. // todo: add these to dioxus
  6. pub trait Reject<E: Clone>: Sized {
  7. fn reject_err(self, t: impl FnOnce(E) -> anyhow::Error) -> Result<Self, anyhow::Error> {
  8. todo!()
  9. }
  10. fn reject_because(self, t: impl Into<String>) -> Result<Self, anyhow::Error> {
  11. todo!()
  12. }
  13. fn reject(self) -> Result<Self, anyhow::Error> {
  14. todo!()
  15. }
  16. }
  17. impl<T, E: Clone> Reject<E> for &Result<T, E> {
  18. fn reject_err(self, t: impl FnOnce(E) -> anyhow::Error) -> Result<Self, anyhow::Error> {
  19. todo!()
  20. }
  21. }
  22. fn use_query_param<'a>(cx: &'a ScopeState) -> Result<&'a i32, ParseError> {
  23. todo!()
  24. }
  25. /// Call "clone" on the underlying error so it can be propogated out
  26. pub trait CloneErr<T, E: ToOwned> {
  27. fn clone_err(&self) -> Result<&T, E::Owned>
  28. where
  29. Self: Sized;
  30. }
  31. impl<E: ToOwned, T> CloneErr<T, E> for Result<T, E> {
  32. fn clone_err(&self) -> Result<&T, E::Owned>
  33. where
  34. Self: Sized,
  35. {
  36. match self {
  37. Ok(s) => Ok(s),
  38. Err(e) => Err(e.to_owned()),
  39. }
  40. }
  41. }
  42. fn app(cx: Scope) -> Element {
  43. // propgates error upwards, does not give a reason, lets Dioxus figure it out
  44. let value = cx.use_hook(|| "123123123.123".parse::<f32>()).reject()?;
  45. // propgates error upwards, gives a reason
  46. let value = cx
  47. .use_hook(|| "123123123.123".parse::<f32>())
  48. .reject_because("Parsing float failed")?;
  49. let value = cx.use_hook(|| "123123123.123".parse::<f32>()).clone_err()?;
  50. let t = use_query_param(cx)?;
  51. let value = cx
  52. .use_hook(|| "123123123.123".parse::<f32>())
  53. .as_ref()
  54. .map_err(|_| anyhow!("Parsing float failed"))?;
  55. todo!()
  56. }