wasm_framework/
error.rs

1//! Errors generated by this crate
2use std::fmt;
3use wasm_bindgen::JsValue;
4
5/// Errors generated by this crate
6/// It's not necessary for users of wasm_service to import this,
7/// because Error implements trait std::error::Error.
8#[derive(Debug)]
9pub enum Error {
10    /// Error serializing/deserializing request, response, or log messages
11    Json(serde_json::Error),
12
13    /// Error converting parameters to/from javascript
14    Js(String),
15
16    /// Error in external http sub-request (via reqwest lib)
17    Http(reqwest::Error),
18
19    /// Error deserializing asset index
20    DeserializeAssets(Box<bincode::ErrorKind>),
21
22    /// Invalid header value (contains non-ascii characters)
23    InvalidHeaderValue(String),
24
25    /// No static asset is available at this path
26    NoStaticAsset(String),
27
28    /// KV asset not found
29    #[allow(clippy::upper_case_acronyms)]
30    KVKeyNotFound(String, u16),
31
32    /// Error received from Cloudflare API while performing KV request
33    #[allow(clippy::upper_case_acronyms)]
34    KVApi(reqwest::Error),
35
36    /// Catch-all
37    Other(String),
38}
39
40impl std::error::Error for Error {}
41unsafe impl Send for Error {}
42
43impl From<String> for Error {
44    fn from(msg: String) -> Error {
45        Error::Other(msg)
46    }
47}
48
49impl From<serde_json::Error> for Error {
50    fn from(e: serde_json::Error) -> Self {
51        Error::Json(e)
52    }
53}
54
55impl From<reqwest::Error> for Error {
56    fn from(e: reqwest::Error) -> Self {
57        Error::Http(e)
58    }
59}
60
61impl From<JsValue> for Error {
62    fn from(e: JsValue) -> Self {
63        Error::Js(
64            e.as_string()
65                .unwrap_or_else(|| "Javascript error".to_string()),
66        )
67    }
68}
69
70impl fmt::Display for Error {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        write!(f, "{:?}", self)
73    }
74}