bulwark_wasm_sdk/
errors.rs

1/// Generic result
2pub type Result = ::std::result::Result<(), Error>;
3
4// TODO: error is way too big, replace w/ aliased anyhow or Box<dyn std::error::Error>
5
6/// Generic error
7pub type Error = ::anyhow::Error;
8
9/// Returned when an attempt to parse a counter within a plugin environment fails.
10#[derive(thiserror::Error, Debug)]
11pub enum ParseCounterError {
12    #[error(transparent)]
13    ParseInt(#[from] std::num::ParseIntError),
14    #[error(transparent)]
15    Utf8(#[from] std::str::Utf8Error),
16}
17
18/// Returned when there was an issue getting or setting a parameter.
19#[derive(thiserror::Error, Debug)]
20pub enum ParamError {
21    #[error("{message}")]
22    Json { message: String },
23}
24
25impl From<crate::bulwark_host::ParamError> for ParamError {
26    fn from(error: crate::bulwark_host::ParamError) -> Self {
27        match error {
28            crate::bulwark_host::ParamError::Json(message) => ParamError::Json { message },
29        }
30    }
31}
32
33/// Returned when there is an issue with the environment variable requested by the plugin.
34#[derive(thiserror::Error, Debug)]
35pub enum EnvVarError {
36    #[error("access to environment variable '{var}' denied")]
37    Permission { var: String },
38    #[error("environment variable '{var}' missing")]
39    Missing { var: String },
40    #[error("{message}")]
41    NotUnicode { message: String },
42}
43
44impl From<crate::bulwark_host::EnvError> for EnvVarError {
45    fn from(error: crate::bulwark_host::EnvError) -> Self {
46        match error {
47            crate::bulwark_host::EnvError::Permission(var) => EnvVarError::Permission { var },
48            crate::bulwark_host::EnvError::Missing(var) => EnvVarError::Missing { var },
49            crate::bulwark_host::EnvError::NotUnicode(var) => EnvVarError::NotUnicode {
50                message: format!("environment variable '{var}' was not unicode"),
51            },
52        }
53    }
54}
55
56impl From<std::string::FromUtf8Error> for EnvVarError {
57    fn from(error: std::string::FromUtf8Error) -> Self {
58        EnvVarError::NotUnicode {
59            message: error.to_string(),
60        }
61    }
62}
63
64/// Returned when there is an issue with the remote state requested by the plugin.
65#[derive(thiserror::Error, Debug)]
66pub enum RemoteStateError {
67    #[error("access to state key '{key}' denied")]
68    Permission { key: String },
69    #[error("error accessing remote state: {message}")]
70    Remote { message: String },
71}
72
73impl From<crate::bulwark_host::StateError> for RemoteStateError {
74    fn from(error: crate::bulwark_host::StateError) -> Self {
75        match error {
76            crate::bulwark_host::StateError::Permission(key) => {
77                RemoteStateError::Permission { key }
78            }
79            crate::bulwark_host::StateError::Remote(message) => {
80                RemoteStateError::Remote { message }
81            }
82        }
83    }
84}
85
86/// Returned when there is an issue with an http request sent by the plugin.
87#[derive(thiserror::Error, Debug)]
88pub enum HttpError {
89    #[error("access to http host '{host}' denied")]
90    Permission { host: String },
91    #[error("invalid http method: '{method}'")]
92    InvalidMethod { method: String },
93    #[error("invalid uri: '{uri}'")]
94    InvalidUri { uri: String },
95    #[error("error sending http request: {message}")]
96    Transmit { message: String },
97    #[error("{message}")]
98    UnavailableContent { message: String },
99    #[error("{message}")]
100    InvalidStart { message: String },
101    #[error("{message}")]
102    ContentTooLarge { message: String },
103}
104
105impl From<crate::bulwark_host::HttpError> for HttpError {
106    fn from(error: crate::bulwark_host::HttpError) -> Self {
107        match error {
108            crate::bulwark_host::HttpError::Permission(host) => HttpError::Permission { host },
109            crate::bulwark_host::HttpError::InvalidMethod(method) => {
110                HttpError::InvalidMethod { method }
111            }
112            crate::bulwark_host::HttpError::InvalidUri(uri) => HttpError::InvalidUri { uri },
113            crate::bulwark_host::HttpError::Transmit(message) => HttpError::Transmit { message },
114            crate::bulwark_host::HttpError::UnavailableContent(message) => {
115                HttpError::UnavailableContent { message }
116            }
117            crate::bulwark_host::HttpError::InvalidStart(message) => {
118                HttpError::InvalidStart { message }
119            }
120            crate::bulwark_host::HttpError::ContentTooLarge(message) => {
121                HttpError::ContentTooLarge { message }
122            }
123        }
124    }
125}