onc_rpc/
errors.rs

1use thiserror::Error;
2
3/// Error types returned by this crate.
4#[non_exhaustive]
5#[derive(Debug, Error, PartialEq)]
6pub enum Error {
7    /// The message header indicates the RPC message is longer than the amount
8    /// of data in the buffer, or the buffer contains more than one message.
9    ///
10    /// This error may also be returned if the RPC message parsed from the
11    /// buffer is unexpectedly shorter than the header length indicates - in
12    /// this case, `buffer_len` will be more than `expected` and may indicate a
13    /// parsing error.
14    #[error("incomplete rpc message (got {buffer_len} bytes, expected {expected})")]
15    IncompleteMessage {
16        /// The length of the buffer provided.
17        buffer_len: usize,
18
19        /// The length expected for this message type.
20        expected: usize,
21    },
22
23    /// The buffer is too small to contain the RPC header.
24    #[error("incomplete fragment header")]
25    IncompleteHeader,
26
27    /// The RPC message is fragmented and needs to be reassembled.
28    ///
29    /// This library doesn't currently support fragmented messages and this
30    /// error will be returned when parsing any message with the "last fragment"
31    /// bit unset in the header.
32    #[error("RPC message is fragmented")]
33    Fragmented,
34
35    /// The message type in the RPC request is neither [`MessageType::Call`]
36    /// or [`MessageType::Reply`].
37    ///
38    /// This is a violation of the spec.
39    ///
40    /// [`MessageType::Call`]: crate::MessageType::Call
41    /// [`MessageType::Reply`]: crate::MessageType::Reply
42    #[error("invalid rpc message type {0}")]
43    InvalidMessageType(u32),
44
45    /// The message type in the RPC request is neither [`ReplyBody::Accepted`]
46    /// or [`ReplyBody::Denied`].
47    ///
48    /// This is a violation of the spec.
49    ///
50    /// [`ReplyBody::Accepted`]: crate::ReplyBody::Accepted
51    /// [`ReplyBody::Denied`]: crate::ReplyBody::Denied
52    #[error("invalid rpc reply type {0}")]
53    InvalidReplyType(u32),
54
55    /// The reply status code is not one of the specified [status
56    /// codes](crate::AcceptedStatus).
57    ///
58    /// This is a violation of the spec.
59    #[error("invalid rpc reply status {0}")]
60    InvalidReplyStatus(u32),
61
62    /// The auth or verifier is invalid or malformed.
63    #[error("invalid rpc auth data")]
64    InvalidAuthData,
65
66    /// The auth error code is not one of the specified [error
67    /// codes](crate::AuthError).
68    ///
69    /// This is a violation of the spec.
70    #[error("invalid rpc auth error status {0}")]
71    InvalidAuthError(u32),
72
73    /// The rejected reply status code is not one of the specified [status
74    /// codes](crate::RejectedReply).
75    ///
76    /// This is a violation of the spec.
77    #[error("invalid rpc rejected reply type {0}")]
78    InvalidRejectedReplyType(u32),
79
80    /// A variable length type has a malformed length value which would exceed
81    /// the length of the buffer.
82    #[error("invalid length in rpc message")]
83    InvalidLength,
84
85    /// The message contains a rpc protocol identifier that is not 2.
86    #[error("invalid rpc version {0}")]
87    InvalidRpcVersion(u32),
88
89    /// The [machine name](crate::auth::AuthUnixParams::machine_name) contains
90    /// non-UTF8 characters.
91    #[error("invalid machine name: {0}")]
92    InvalidMachineName(#[from] std::str::Utf8Error),
93
94    /// An I/O error occurred when trying to parse the buffer.
95    #[error("i/o error ({0:?}): {1}")]
96    IOError(std::io::ErrorKind, String),
97}
98
99impl From<std::io::Error> for Error {
100    fn from(v: std::io::Error) -> Self {
101        Self::IOError(v.kind(), v.to_string())
102    }
103}