wasmrs_frames/
error.rs

1//! Library-specific error types and utility functions
2
3use bytes::Bytes;
4
5use crate::frames::ErrorCode;
6
7/// Error type for wasmRS RSocket errors.
8#[allow(missing_copy_implementations)]
9#[derive(Debug, Clone)]
10pub enum Error {
11  /// An error associated with OperationList methods.
12  OpList(String),
13  /// A generic RSocket error.
14  RSocket(u32),
15  /// Used when the receiver for a [crate::WasmSocket] has already been taken.
16  ReceiverAlreadyGone,
17  /// Variant used when a frame is treated as the wrong type.
18  WrongType,
19  /// Could not convert string from passed bytes.
20  StringConversion,
21  /// Did not find necessary [crate::Metadata] on a payload.
22  MetadataNotFound,
23  /// A problem with extra metadata on [crate::Metadata].
24  Extra(String),
25}
26
27/// A utility method for creating an Error::Extra variant.
28pub fn ex_err(msg: impl AsRef<str>) -> Error {
29  Error::Extra(msg.as_ref().to_owned())
30}
31
32impl std::error::Error for Error {}
33impl std::fmt::Display for Error {
34  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35    match self {
36      Error::RSocket(code) => f.write_str((Into::<u32>::into(*code)).to_string().as_str()),
37      Error::OpList(msg) => f.write_str(msg),
38      Error::ReceiverAlreadyGone => f.write_str("Received already taken"),
39      Error::WrongType => f.write_str("Tried to decode frame with wrong frame decoder"),
40      Error::StringConversion => f.write_str("Could not read string bytes"),
41      Error::Extra(m) => f.write_str(m),
42      Error::MetadataNotFound => f.write_str("Metadata missing"),
43    }
44  }
45}
46
47#[derive(Debug, Clone)]
48#[cfg_attr(feature = "derive_serde", derive(serde::Serialize, serde::Deserialize))]
49#[must_use]
50/// The error type used for all [wasmrs_rx::Mono]/[wasmrs_rx::Flux] payloads.
51pub struct PayloadError {
52  /// The error code.
53  pub code: u32,
54  /// Metadata associated with the error.
55  pub metadata: Option<Bytes>,
56  /// The error message.
57  pub msg: String,
58}
59
60impl PayloadError {
61  /// Create a new [PayloadError] with the passed code and message.
62  pub fn new(code: u32, msg: impl AsRef<str>, metadata: Option<Bytes>) -> Self {
63    Self {
64      code,
65      metadata,
66      msg: msg.as_ref().to_owned(),
67    }
68  }
69
70  /// Create a new [PayloadError] with the [ErrorCode::ApplicationError] code.
71  pub fn application_error(msg: impl AsRef<str>, metadata: Option<Bytes>) -> Self {
72    Self {
73      code: ErrorCode::ApplicationError.into(),
74      metadata,
75      msg: msg.as_ref().to_owned(),
76    }
77  }
78}
79impl std::error::Error for PayloadError {}
80impl std::fmt::Display for PayloadError {
81  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82    f.write_str(&self.msg)
83  }
84}
85
86impl From<Error> for PayloadError {
87  fn from(e: Error) -> Self {
88    app_err(&e)
89  }
90}
91
92impl From<Box<dyn std::error::Error>> for PayloadError {
93  fn from(e: Box<dyn std::error::Error>) -> Self {
94    app_err(e.as_ref())
95  }
96}
97
98impl From<Box<dyn std::error::Error + Send + Sync>> for PayloadError {
99  fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
100    app_err(e.as_ref())
101  }
102}
103
104fn app_err(e: &dyn std::error::Error) -> PayloadError {
105  PayloadError::application_error(e.to_string(), None)
106}