mesh_channel_core/
error.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use thiserror::Error;
5
6/// An error representing a failure of a channel.
7#[derive(Debug, Error)]
8#[error(transparent)]
9pub struct ChannelError(Box<ChannelErrorInner>);
10
11/// The kind of channel failure.
12#[derive(Debug)]
13#[non_exhaustive]
14pub enum ChannelErrorKind {
15    /// The peer node failed.
16    NodeFailure,
17    /// The received message contents are invalid.
18    Corruption,
19}
20
21impl ChannelError {
22    /// Returns the kind of channel failure that occurred.
23    pub fn kind(&self) -> ChannelErrorKind {
24        match &*self.0 {
25            ChannelErrorInner::NodeFailure(_) => ChannelErrorKind::NodeFailure,
26            ChannelErrorInner::Corruption(_) => ChannelErrorKind::Corruption,
27        }
28    }
29}
30
31impl From<mesh_protobuf::Error> for ChannelError {
32    fn from(err: mesh_protobuf::Error) -> Self {
33        Self(Box::new(ChannelErrorInner::Corruption(err)))
34    }
35}
36
37impl From<mesh_node::local_node::NodeError> for ChannelError {
38    fn from(value: mesh_node::local_node::NodeError) -> Self {
39        Self(Box::new(ChannelErrorInner::NodeFailure(value)))
40    }
41}
42
43#[derive(Debug, Error)]
44enum ChannelErrorInner {
45    #[error("node failure")]
46    NodeFailure(#[source] mesh_node::local_node::NodeError),
47    #[error("message corruption")]
48    Corruption(#[source] mesh_protobuf::Error),
49}
50
51/// An error when trying to receive a message from a channel.
52#[derive(Debug, Error)]
53pub enum TryRecvError {
54    /// The channel is empty.
55    #[error("channel empty")]
56    Empty,
57    /// The channel is closed.
58    #[error("channel closed")]
59    Closed,
60    /// The channel has failed.
61    #[error("channel failure")]
62    Error(#[from] ChannelError),
63}
64
65/// An error when receiving a message from a channel.
66#[derive(Debug, Error)]
67pub enum RecvError {
68    /// The channel is closed.
69    #[error("channel closed")]
70    Closed,
71    /// The channel has failed.
72    #[error("channel failure")]
73    Error(#[from] ChannelError),
74}