mesh_channel_core/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use thiserror::Error;

/// An error representing a failure of a channel.
#[derive(Debug, Error)]
#[error(transparent)]
pub struct ChannelError(Box<ChannelErrorInner>);

/// The kind of channel failure.
#[derive(Debug)]
#[non_exhaustive]
pub enum ChannelErrorKind {
    /// The peer node failed.
    NodeFailure,
    /// The received message contents are invalid.
    Corruption,
}

impl ChannelError {
    /// Returns the kind of channel failure that occurred.
    pub fn kind(&self) -> ChannelErrorKind {
        match &*self.0 {
            ChannelErrorInner::NodeFailure(_) => ChannelErrorKind::NodeFailure,
            ChannelErrorInner::Corruption(_) => ChannelErrorKind::Corruption,
        }
    }
}

impl From<mesh_protobuf::Error> for ChannelError {
    fn from(err: mesh_protobuf::Error) -> Self {
        Self(Box::new(ChannelErrorInner::Corruption(err)))
    }
}

impl From<mesh_node::local_node::NodeError> for ChannelError {
    fn from(value: mesh_node::local_node::NodeError) -> Self {
        Self(Box::new(ChannelErrorInner::NodeFailure(value)))
    }
}

#[derive(Debug, Error)]
enum ChannelErrorInner {
    #[error("node failure")]
    NodeFailure(#[source] mesh_node::local_node::NodeError),
    #[error("message corruption")]
    Corruption(#[source] mesh_protobuf::Error),
}

/// An error when trying to receive a message from a channel.
#[derive(Debug, Error)]
pub enum TryRecvError {
    /// The channel is empty.
    #[error("channel empty")]
    Empty,
    /// The channel is closed.
    #[error("channel closed")]
    Closed,
    /// The channel has failed.
    #[error("channel failure")]
    Error(#[from] ChannelError),
}

/// An error when receiving a message from a channel.
#[derive(Debug, Error)]
pub enum RecvError {
    /// The channel is closed.
    #[error("channel closed")]
    Closed,
    /// The channel has failed.
    #[error("channel failure")]
    Error(#[from] ChannelError),
}