wick_packet/
error.rs

1use serde_json::Value;
2use wick_interface_types::Type;
3
4use crate::PacketError;
5
6/// Errors originating from WASM components.
7#[derive(Debug, thiserror::Error, PartialEq, Clone)]
8#[non_exhaustive]
9pub enum Error {
10  /// Thrown when a user attempts to retrieve a stream for a port that doesn't exist.
11  #[error("No stream found for port '{0}'")]
12  PortMissing(String),
13
14  /// Error deserializing payload.
15  #[error("Error deserializing payload '{}': {}",.as_json,.error)]
16  Decode { as_json: String, error: String },
17
18  /// Error converting payload into JSON.
19  #[error("Error JSON-ifying payload: {0}")]
20  Jsonify(String),
21
22  /// Error communicating over a stream or channel.
23  #[error("Error communicating over a stream or channel: {0}")]
24  Channel(String),
25
26  /// General error to wrap other errors.
27  #[error("{0}")]
28  Component(String),
29
30  /// Payload was successful but no data was provided.
31  #[error("No data in payload")]
32  NoData,
33
34  /// An error that wraps a PayloadError.
35  #[error("{}", .0.msg())]
36  PayloadError(PacketError),
37
38  /// Thrown when a user attempts to use a signal when they expected a payload.
39  #[error("Got a Done signal in an unexpected context.")]
40  UnexpectedDone,
41
42  /// Returned when an operation attempts to retrieve a configuration item that doesn't exist or decoding fails.
43  #[error("Could not retrieve configuration item '{0}'")]
44  ContextKey(String),
45
46  /// Returned when trying to decode a non-JSON object into [crate::RuntimeConfig].
47  #[error("Can only convert JSON Objects to a operation and component configuration, got '{0}'")]
48  BadJson(Value),
49
50  /// Couldn't retrieve a complete set of packets from a [crate::StreamMap]
51  #[error("Could not retrieve a complete set of packets. Stream '{0}' failed to provide a packet: '{1}'")]
52  StreamMapError(String /* port */, String /* error */),
53
54  /// Couldn't retrieve a complete set of packets from a [crate::StreamMap]
55  #[error("Could not retrieve a complete set of packets. Stream '{0}' completed or failed before providing a packet.")]
56  StreamMapMissing(String /* port */),
57
58  #[error("Configuration provided for component '{0}' does not match expected signature, {1}")]
59  Signature(String, String),
60
61  #[cfg(feature = "datetime")]
62  #[error("Error parsing date '{0}', date must be an RFC 3339 formatted string")]
63  ParseDate(String),
64
65  #[cfg(feature = "datetime")]
66  #[error("Error parsing date '{0}', date must be milliseconds from the UNIX epoch")]
67  ParseDateMillis(u64),
68
69  #[error("Could not coerce value {value} to a {desired}")]
70  Coersion { value: Value, desired: Type },
71}
72
73impl Error {
74  pub fn component_error<T: Into<String>>(msg: T) -> Self {
75    Self::Component(msg.into())
76  }
77}
78
79impl From<wasmrs_rx::Error> for Error {
80  fn from(value: wasmrs_rx::Error) -> Self {
81    Self::Channel(value.to_string())
82  }
83}
84
85impl From<Box<dyn std::error::Error>> for Error {
86  fn from(value: Box<dyn std::error::Error>) -> Self {
87    Self::Component(value.to_string())
88  }
89}
90
91#[derive(thiserror::Error, Debug)]
92#[non_exhaustive]
93/// The error type for Wick Entities.
94pub enum ParseError {
95  /// Encountered an invalid scheme when parsing an entity URL.
96  #[error("Invalid scheme {0}")]
97  Scheme(String),
98  /// No path supplied in the entity URL.
99  #[error("Missing path")]
100  MissingPath,
101  /// No authority/host supplied in the entity URL.
102  #[error("Missing authority/host")]
103  Authority,
104  /// Invalid authority/host supplied in the entity URL.
105  #[error("Invalid authority/host '{0}', missing separator '.'")]
106  InvalidAuthority(String),
107  /// Invalid authority/host kind.
108  #[error("Invalid authority/host kind '{0}'")]
109  InvalidAuthorityKind(String),
110  /// Error parsing an entity URL.
111  #[error("{0}")]
112  Parse(url::ParseError),
113  /// Error converting arguments into an [crate::Entity].
114  #[error(transparent)]
115  Conversion(Box<dyn std::error::Error + Send + Sync>),
116}