netlink_packet_utils/
errors.rs

1// SPDX-License-Identifier: MIT
2
3use anyhow::anyhow;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7#[error("Encode error occurred: {inner}")]
8pub struct EncodeError {
9    inner: anyhow::Error,
10}
11
12impl From<&'static str> for EncodeError {
13    fn from(msg: &'static str) -> Self {
14        EncodeError { inner: anyhow!(msg) }
15    }
16}
17
18impl From<String> for EncodeError {
19    fn from(msg: String) -> Self {
20        EncodeError { inner: anyhow!(msg) }
21    }
22}
23
24impl From<anyhow::Error> for EncodeError {
25    fn from(inner: anyhow::Error) -> EncodeError {
26        EncodeError { inner }
27    }
28}
29
30#[derive(Debug, Error)]
31pub enum DecodeError {
32    #[error("Invalid MAC address")]
33    InvalidMACAddress,
34
35    #[error("Invalid IP address")]
36    InvalidIPAddress,
37
38    #[error("Invalid string")]
39    Utf8Error(#[from] std::string::FromUtf8Error),
40
41    #[error("Invalid u8")]
42    InvalidU8,
43
44    #[error("Invalid u16")]
45    InvalidU16,
46
47    #[error("Invalid u32")]
48    InvalidU32,
49
50    #[error("Invalid u64")]
51    InvalidU64,
52
53    #[error("Invalid u128")]
54    InvalidU128,
55
56    #[error("Invalid i32")]
57    InvalidI32,
58
59    #[error("Invalid {name}: length {len} < {buffer_len}")]
60    InvalidBufferLength { name: &'static str, len: usize, buffer_len: usize },
61
62    #[error(transparent)]
63    Nla(#[from] crate::nla::NlaError),
64
65    #[error(transparent)]
66    Other(#[from] anyhow::Error),
67
68    #[error("Failed to parse NLMSG_ERROR: {0}")]
69    FailedToParseNlMsgError(Box<DecodeError>),
70
71    #[error("Failed to parse NLMSG_DONE: {0}")]
72    FailedToParseNlMsgDone(Box<DecodeError>),
73
74    #[error("Failed to parse message with type {message_type}: {source}")]
75    FailedToParseMessageWithType { message_type: u16, source: Box<DecodeError> },
76
77    #[error("Failed to parse netlink header: {0}")]
78    FailedToParseNetlinkHeader(Box<DecodeError>),
79}
80
81impl From<&'static str> for DecodeError {
82    fn from(msg: &'static str) -> Self {
83        DecodeError::Other(anyhow!(msg))
84    }
85}
86
87impl From<String> for DecodeError {
88    fn from(msg: String) -> Self {
89        DecodeError::Other(anyhow!(msg))
90    }
91}