bulwark_wasm_sdk/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/// Generic result
pub type Result = ::std::result::Result<(), Error>;

// TODO: error is way too big, replace w/ aliased anyhow or Box<dyn std::error::Error>

/// Generic error
pub type Error = ::anyhow::Error;

/// Returned when an attempt to parse a counter within a plugin environment fails.
#[derive(thiserror::Error, Debug)]
pub enum ParseCounterError {
    #[error(transparent)]
    ParseInt(#[from] std::num::ParseIntError),
    #[error(transparent)]
    Utf8(#[from] std::str::Utf8Error),
}

/// Returned when there was an issue getting or setting a parameter.
#[derive(thiserror::Error, Debug)]
pub enum ParamError {
    #[error("{message}")]
    Json { message: String },
}

impl From<crate::bulwark_host::ParamError> for ParamError {
    fn from(error: crate::bulwark_host::ParamError) -> Self {
        match error {
            crate::bulwark_host::ParamError::Json(message) => ParamError::Json { message },
        }
    }
}

/// Returned when there is an issue with the environment variable requested by the plugin.
#[derive(thiserror::Error, Debug)]
pub enum EnvVarError {
    #[error("access to environment variable '{var}' denied")]
    Permission { var: String },
    #[error("environment variable '{var}' missing")]
    Missing { var: String },
    #[error("{message}")]
    NotUnicode { message: String },
}

impl From<crate::bulwark_host::EnvError> for EnvVarError {
    fn from(error: crate::bulwark_host::EnvError) -> Self {
        match error {
            crate::bulwark_host::EnvError::Permission(var) => EnvVarError::Permission { var },
            crate::bulwark_host::EnvError::Missing(var) => EnvVarError::Missing { var },
            crate::bulwark_host::EnvError::NotUnicode(var) => EnvVarError::NotUnicode {
                message: format!("environment variable '{var}' was not unicode"),
            },
        }
    }
}

impl From<std::string::FromUtf8Error> for EnvVarError {
    fn from(error: std::string::FromUtf8Error) -> Self {
        EnvVarError::NotUnicode {
            message: error.to_string(),
        }
    }
}

/// Returned when there is an issue with the remote state requested by the plugin.
#[derive(thiserror::Error, Debug)]
pub enum RemoteStateError {
    #[error("access to state key '{key}' denied")]
    Permission { key: String },
    #[error("error accessing remote state: {message}")]
    Remote { message: String },
}

impl From<crate::bulwark_host::StateError> for RemoteStateError {
    fn from(error: crate::bulwark_host::StateError) -> Self {
        match error {
            crate::bulwark_host::StateError::Permission(key) => {
                RemoteStateError::Permission { key }
            }
            crate::bulwark_host::StateError::Remote(message) => {
                RemoteStateError::Remote { message }
            }
        }
    }
}

/// Returned when there is an issue with an http request sent by the plugin.
#[derive(thiserror::Error, Debug)]
pub enum HttpError {
    #[error("access to http host '{host}' denied")]
    Permission { host: String },
    #[error("invalid http method: '{method}'")]
    InvalidMethod { method: String },
    #[error("invalid uri: '{uri}'")]
    InvalidUri { uri: String },
    #[error("error sending http request: {message}")]
    Transmit { message: String },
    #[error("{message}")]
    UnavailableContent { message: String },
    #[error("{message}")]
    InvalidStart { message: String },
    #[error("{message}")]
    ContentTooLarge { message: String },
}

impl From<crate::bulwark_host::HttpError> for HttpError {
    fn from(error: crate::bulwark_host::HttpError) -> Self {
        match error {
            crate::bulwark_host::HttpError::Permission(host) => HttpError::Permission { host },
            crate::bulwark_host::HttpError::InvalidMethod(method) => {
                HttpError::InvalidMethod { method }
            }
            crate::bulwark_host::HttpError::InvalidUri(uri) => HttpError::InvalidUri { uri },
            crate::bulwark_host::HttpError::Transmit(message) => HttpError::Transmit { message },
            crate::bulwark_host::HttpError::UnavailableContent(message) => {
                HttpError::UnavailableContent { message }
            }
            crate::bulwark_host::HttpError::InvalidStart(message) => {
                HttpError::InvalidStart { message }
            }
            crate::bulwark_host::HttpError::ContentTooLarge(message) => {
                HttpError::ContentTooLarge { message }
            }
        }
    }
}