wasmcloud_provider_sdk/
error.rs

1//! Error types for interacting with a provider
2
3pub type InvocationResult<T> = Result<T, InvocationError>;
4pub type ProviderInitResult<T> = Result<T, ProviderInitError>;
5
6/// All errors that that can be returned by a provider when it is being initialized,
7/// primarily from internal provider-sdk code
8#[derive(Debug, thiserror::Error)]
9pub enum ProviderInitError {
10    /// Errors when connecting to the lattice NATS cluster
11    #[error(transparent)]
12    Connect(#[from] async_nats::ConnectError),
13    /// An error that occurs when subscribing to or interacting with RPC topics
14    #[error(transparent)]
15    Subscription(#[from] async_nats::SubscribeError),
16    /// Initialization error when setting up a provider (such as invalid information or configuration)
17    #[error("Initialization error: {0}")]
18    Initialization(String),
19}
20
21/// Errors that can occur when sending or receiving an invocation, including the `dispatch` method
22/// of the provider.
23#[derive(Debug, thiserror::Error)]
24#[non_exhaustive]
25pub enum InvocationError {
26    /// Indicates that validation for the invocation failed
27    #[error(transparent)]
28    Validation(#[from] ValidationError),
29    /// The invocation or dispatch timed out
30    #[error("Invocation timed out")]
31    Timeout,
32    /// The invocation or dispatch failed when serializing data from the wire
33    #[error("Error when serializing invocation: {0:?}")]
34    // NOTE(thomastaylor312): we might have to just make this and `Deser` a string with some
35    // convenience `From` implementations if we do have to indicate other failures other than
36    // serialization to our wasmbus RPC messages
37    Ser(#[from] rmp_serde::encode::Error),
38    #[error("Error while serializing/deserializing JSON: {0:?}")]
39    /// Serialization/Deserializing errors that occur for JSON
40    SerdeJson(#[from] serde_json::Error),
41    /// The invocation or dispatch failed when deserializing data from the wire
42    #[error("Error when deserializing invocation: {0:?}")]
43    Deser(#[from] rmp_serde::decode::Error),
44    /// An error that occurred when trying to publish or request over NATS
45    #[error("Networking error during invocation: {0:?}")]
46    Network(#[from] NetworkError),
47    /// Errors that occur when chunking data
48    #[error("Error when chunking data: {0}")]
49    Chunking(String),
50    /// Returned when an invocation is malformed (e.g. has a method type that isn't supported)
51    #[error("Malformed invocation: {0}")]
52    Malformed(String),
53    /// Returned when an invocation returns an error
54    #[error("Unexpected error: {0}")]
55    Unexpected(String),
56}
57
58/// All errors that can occur when validating an invocation
59#[derive(Debug, thiserror::Error)]
60pub enum ValidationError {
61    /// Issuer of the invocation is not authorized for this cluster
62    #[error("Issuer of this invocation is not in list of cluster issuers")]
63    InvalidIssuer,
64    /// The target of the invocation is not the same as the provider
65    #[error("Target of the invocation was {0}, which does not match the provider {1}")]
66    InvalidTarget(String, String),
67    /// The component that sent the request is not linked to the provider
68    #[error("Component {0} is not linked to this provider")]
69    InvalidComponent(String),
70    // Claims have expired
71    #[error("Invocation claims token expired")]
72    Expired,
73    /// The signature on the claims is invalid
74    #[error("Invocation claims signature invalid")]
75    InvalidSignature,
76    /// Claims are not valid yet. This occurs when the `nbf` field is in the future
77    #[error("Invocation claims not valid yet")]
78    NotValidYet,
79    /// Wascap metadata is not present
80    #[error("Invocation claims missing wascap metadata")]
81    MissingWascapClaims,
82    /// The hash on the invocation doesn't match the hash on the claims
83    #[error("Invocation hash does not match claims hash")]
84    HashMismatch,
85    /// The claims are not valid JSON
86    #[error("Invocation claims are not valid JSON")]
87    InvalidJson(String),
88    /// Host ID is not a valid nkey identity
89    #[error("Invalid host ID: {0}")]
90    InvalidHostId(String),
91    /// The target of the invocation is not valid
92    #[error("Invocation claims and invocation target URL do not match: {0} != {1}")]
93    InvalidTargetUrl(String, String),
94    /// The origin of the invocation is not valid
95    #[error("Invocation claims and invocation origin URL do not match: {0} != {1}")]
96    InvalidOriginUrl(String, String),
97}
98
99/// This is a wrapper around two different NATS errors that we use (publish and request). It
100/// delegates to the underlying error types from NATS
101#[derive(Debug, thiserror::Error)]
102pub enum NetworkError {
103    #[error(transparent)]
104    Publish(#[from] async_nats::PublishError),
105    #[error(transparent)]
106    Request(#[from] async_nats::RequestError),
107}