Skip to main content

dioxus_cloudflare/
error.rs

1//! Error bridge between `worker::Error` and Dioxus `ServerFnError`.
2//!
3//! Rust's orphan rule prevents implementing `From<worker::Error> for ServerFnError`
4//! directly since neither type is defined in this crate. The [`CfError`] newtype
5//! and [`CfResultExt`] trait provide an ergonomic workaround.
6
7use std::fmt;
8
9/// Newtype wrapper around [`worker::Error`] that implements conversion to
10/// `ServerFnError`.
11///
12/// Use the [`.cf()`](CfResultExt::cf) method on `worker::Result<T>` instead
13/// of constructing this directly.
14#[derive(Debug)]
15pub struct CfError(pub worker::Error);
16
17impl fmt::Display for CfError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "Worker error: {}", self.0)
20    }
21}
22
23impl std::error::Error for CfError {}
24
25impl From<CfError> for crate::ServerFnError {
26    fn from(e: CfError) -> Self {
27        Self::new(e.to_string())
28    }
29}
30
31/// Extension trait for `worker::Result<T>` that converts errors into a type
32/// compatible with `ServerFnError` via the `?` operator.
33///
34/// # Example
35///
36/// ```rust,ignore
37/// use dioxus_cloudflare::prelude::*;
38///
39/// #[server]
40/// pub async fn get_count() -> Result<i64, ServerFnError> {
41///     let kv = cf::env().kv("COUNTS").cf()?;
42///     let val = kv.get("total").text().await.cf()?;
43///     Ok(val.unwrap_or_default().parse().unwrap_or(0))
44/// }
45/// ```
46pub trait CfResultExt<T> {
47    /// Convert a `worker::Result<T>` into `Result<T, CfError>`, which can
48    /// then be propagated with `?` into any function returning
49    /// `Result<_, ServerFnError>`.
50    fn cf(self) -> Result<T, CfError>;
51}
52
53impl<T> CfResultExt<T> for Result<T, worker::Error> {
54    fn cf(self) -> Result<T, CfError> {
55        self.map_err(CfError)
56    }
57}
58
59impl<T> CfResultExt<T> for Result<T, worker::kv::KvError> {
60    fn cf(self) -> Result<T, CfError> {
61        self.map_err(|e| CfError(e.into()))
62    }
63}