momento_functions_host/
error.rs

1//! Common host interfaces types
2
3use momento_functions_wit::host::momento::functions::types::InvocationError;
4
5/// An alias for Result<T, Error> for convenience.
6pub type FunctionResult<T> = std::result::Result<T, Error>;
7
8/// An error during the execution of a Function.
9#[derive(thiserror::Error, Debug)]
10pub enum Error {
11    /// A low-level WIT error during the invocation of a Function.
12    #[error("Invocation error: {0}")]
13    InvocationError(#[from] InvocationError),
14
15    /// A catch-all error with a message.
16    #[error("{0}")]
17    MessageError(String),
18}
19
20impl From<&str> for Error {
21    fn from(msg: &str) -> Self {
22        Error::MessageError(msg.to_string())
23    }
24}
25impl From<String> for Error {
26    fn from(msg: String) -> Self {
27        Error::MessageError(msg)
28    }
29}
30
31impl From<Error> for InvocationError {
32    fn from(e: Error) -> Self {
33        match e {
34            Error::InvocationError(e) => e,
35            Error::MessageError(msg) => InvocationError::RequestError(msg),
36        }
37    }
38}
39
40impl From<Error>
41    for momento_functions_wit::function_web::momento::functions::types::InvocationError
42{
43    fn from(e: Error) -> Self {
44        match e {
45            Error::InvocationError(e) => momento_functions_wit::function_web::momento::functions::types::InvocationError::RequestError(e.to_string()),
46            Error::MessageError(msg) => momento_functions_wit::function_web::momento::functions::types::InvocationError::RequestError(msg),
47        }
48    }
49}
50
51impl From<Error>
52    for momento_functions_wit::function_spawn::momento::functions::types::InvocationError
53{
54    fn from(e: Error) -> Self {
55        match e {
56            Error::InvocationError(e) => momento_functions_wit::function_spawn::momento::functions::types::InvocationError::RequestError(e.to_string()),
57            Error::MessageError(msg) => momento_functions_wit::function_spawn::momento::functions::types::InvocationError::RequestError(msg),
58        }
59    }
60}