memberlist_proto/proto/
error.rs

1use crate::{DecodeError, EncodeError, Label, ParseLabelError};
2
3/// An error that can occur during encoding.
4#[derive(Debug, thiserror::Error)]
5pub enum ProtoDecoderError {
6  /// Decoding error
7  #[error(transparent)]
8  Decode(#[from] DecodeError),
9  /// Returned when failed to parse the label.
10  #[error(transparent)]
11  Label(#[from] ParseLabelError),
12  /// The label does not match the expected label
13  #[error("The label {actual} does not match the expected label {expected}")]
14  LabelMismatch {
15    /// The actual label
16    actual: Label,
17    /// The expected label
18    expected: Label,
19  },
20  /// Required label is missing
21  #[error("label not found")]
22  LabelNotFound,
23  /// unexpected double packet label header
24  #[error("unexpected double packet label header")]
25  UnexpectedLabel,
26
27  /// The offload task is canceled
28  #[error("The offload task is canceled")]
29  Offload,
30
31  /// Returned when the encryption feature is disabled.
32  #[error(
33    "Receive an encrypted message while the encryption feature is disabled on the running node"
34  )]
35  EncryptionDisabled,
36
37  /// Returned when the checksum feature is disabled.
38  #[error(
39    "Receive a checksumed message while the checksum feature is disabled on the running node"
40  )]
41  ChecksumDisabled,
42
43  /// Returned when the compression feature is disabled.
44  #[error(
45    "Receive a compressed message while the compression feature is disabled on the running node"
46  )]
47  CompressionDisabled,
48
49  /// Encryption error
50  #[cfg(feature = "encryption")]
51  #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
52  #[error(transparent)]
53  Encryption(#[from] crate::EncryptionError),
54  /// Not encrypted
55  #[cfg(feature = "encryption")]
56  #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
57  #[error("The message is not encrypted")]
58  NotEncrypted,
59  /// No installed key
60  #[cfg(feature = "encryption")]
61  #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
62  #[error("key not found for decryption")]
63  SecretKeyNotFound,
64  /// No installed keys could decrypt the message
65  #[cfg(feature = "encryption")]
66  #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
67  #[error("no installed keys could decrypt the message")]
68  NoInstalledKeys,
69
70  /// Compression error
71  #[cfg(any(
72    feature = "zstd",
73    feature = "lz4",
74    feature = "snappy",
75    feature = "brotli",
76  ))]
77  #[cfg_attr(
78    docsrs,
79    doc(cfg(any(
80      feature = "zstd",
81      feature = "lz4",
82      feature = "snappy",
83      feature = "brotli",
84    )))
85  )]
86  #[error(transparent)]
87  Compression(#[from] crate::CompressionError),
88
89  /// Checksum error
90  #[cfg(any(
91    feature = "crc32",
92    feature = "xxhash32",
93    feature = "xxhash64",
94    feature = "xxhash3",
95    feature = "murmur3",
96  ))]
97  #[cfg_attr(
98    docsrs,
99    doc(cfg(any(
100      feature = "crc32",
101      feature = "xxhash64",
102      feature = "xxhash32",
103      feature = "xxhash3",
104      feature = "murmur3",
105    )))
106  )]
107  #[error(transparent)]
108  Checksum(#[from] crate::ChecksumError),
109}
110
111impl From<ProtoDecoderError> for std::io::Error {
112  fn from(value: ProtoDecoderError) -> Self {
113    use std::io::ErrorKind;
114
115    match value {
116      ProtoDecoderError::Decode(e) => Self::new(ErrorKind::InvalidData, e),
117      ProtoDecoderError::Label(e) => Self::new(ErrorKind::InvalidData, e),
118      ProtoDecoderError::LabelMismatch { .. } => Self::new(ErrorKind::InvalidData, value),
119      ProtoDecoderError::Offload => Self::other(value),
120      ProtoDecoderError::EncryptionDisabled => Self::other(value),
121      ProtoDecoderError::ChecksumDisabled => Self::other(value),
122      ProtoDecoderError::CompressionDisabled => Self::other(value),
123      ProtoDecoderError::UnexpectedLabel => Self::new(ErrorKind::InvalidInput, value),
124      ProtoDecoderError::LabelNotFound => Self::new(ErrorKind::InvalidInput, value),
125      #[cfg(feature = "encryption")]
126      ProtoDecoderError::Encryption(e) => Self::new(ErrorKind::InvalidData, e),
127      #[cfg(feature = "encryption")]
128      ProtoDecoderError::NotEncrypted => Self::new(ErrorKind::InvalidData, value),
129      #[cfg(feature = "encryption")]
130      ProtoDecoderError::SecretKeyNotFound => Self::new(ErrorKind::InvalidData, value),
131      #[cfg(feature = "encryption")]
132      ProtoDecoderError::NoInstalledKeys => Self::new(ErrorKind::InvalidData, value),
133      #[cfg(any(
134        feature = "zstd",
135        feature = "lz4",
136        feature = "snappy",
137        feature = "brotli",
138      ))]
139      ProtoDecoderError::Compression(e) => Self::new(ErrorKind::InvalidData, e),
140      #[cfg(any(
141        feature = "crc32",
142        feature = "xxhash32",
143        feature = "xxhash64",
144        feature = "xxhash3",
145        feature = "murmur3",
146      ))]
147      ProtoDecoderError::Checksum(e) => Self::new(ErrorKind::InvalidData, e),
148    }
149  }
150}
151
152impl ProtoDecoderError {
153  /// Creates a new label mismatch error.
154  #[inline]
155  pub(crate) const fn label_mismatch(expected: Label, actual: Label) -> Self {
156    Self::LabelMismatch { expected, actual }
157  }
158
159  #[inline]
160  pub(crate) const fn double_label() -> Self {
161    Self::UnexpectedLabel
162  }
163}
164
165/// The errors may occur during encoding.
166#[derive(Debug, thiserror::Error)]
167pub enum ProtoEncoderError {
168  #[error(transparent)]
169  Encode(#[from] EncodeError),
170  #[cfg(feature = "encryption")]
171  #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
172  #[error(transparent)]
173  Encrypt(#[from] crate::EncryptionError),
174  #[cfg(any(
175    feature = "zstd",
176    feature = "lz4",
177    feature = "brotli",
178    feature = "snappy",
179  ))]
180  #[cfg_attr(
181    docsrs,
182    feature(any(
183      feature = "zstd",
184      feature = "lz4",
185      feature = "brotli",
186      feature = "snappy",
187    ))
188  )]
189  #[error(transparent)]
190  Compress(#[from] crate::CompressionError),
191  #[cfg(any(
192    feature = "crc32",
193    feature = "xxhash32",
194    feature = "xxhash64",
195    feature = "xxhash3",
196    feature = "murmur3",
197  ))]
198  #[cfg_attr(
199    docsrs,
200    feature(any(
201      feature = "crc32",
202      feature = "xxhash32",
203      feature = "xxhash64",
204      feature = "xxhash3",
205      feature = "murmur3",
206    ))
207  )]
208  #[error(transparent)]
209  Checksum(#[from] crate::ChecksumError),
210}