1use std::fmt;
2
3use crate::netstack::Error as NetstackError;
4
5#[derive(Debug, thiserror::Error, Clone, Copy, Eq, PartialEq)]
7pub enum Error {
8 #[error("operation timed-out")]
12 Timeout,
13
14 #[error("connection reset")]
18 ConnectionReset,
19
20 #[error("an error reading or parsing the key file")]
22 KeyFileRead,
23
24 #[error("an error writing out the key file")]
26 KeyFileWrite,
27
28 #[error("the environment variable `{}` was not set", crate::ENV_MAGIC_VAR)]
34 UnstableEnvVar,
35
36 #[error("internal error ({0})")]
43 Internal(InternalErrorKind),
44}
45
46#[non_exhaustive]
48#[derive(Debug, Clone, Copy, Eq, PartialEq)]
49pub enum InternalErrorKind {
50 InvalidSocketState,
52 InternalResponseMismatch,
54 InternalChannelClosed,
56 BadListenerHandle,
58 BadSocketHandle,
60 BadRequest,
62 BufferFull,
64 Actor,
66 UnsupportedInTunMode,
70 NotFound,
72 AlreadyExists,
74 Io,
76}
77
78impl fmt::Display for InternalErrorKind {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match self {
81 InternalErrorKind::InvalidSocketState => write!(f, "invalid socket state"),
82 InternalErrorKind::InternalResponseMismatch => {
83 write!(f, "response type mismatched to request type")
84 }
85 InternalErrorKind::InternalChannelClosed => write!(f, "channel closed"),
86 InternalErrorKind::BadListenerHandle => write!(f, "handle to invalid TCP listener"),
87 InternalErrorKind::BadSocketHandle => write!(f, "handle to invalid socket"),
88 InternalErrorKind::BadRequest => write!(f, "bad request"),
89 InternalErrorKind::BufferFull => write!(f, "buffer full"),
90 InternalErrorKind::Actor => write!(f, "actor missing or shutdown"),
91 InternalErrorKind::UnsupportedInTunMode => {
92 write!(f, "operation unsupported in TUN transport mode")
93 }
94 InternalErrorKind::NotFound => write!(f, "resource not found"),
95 InternalErrorKind::AlreadyExists => write!(f, "resource already exists"),
96 InternalErrorKind::Io => write!(f, "I/O error"),
97 }
98 }
99}
100
101impl From<crate::netstack::InternalErrorKind> for InternalErrorKind {
102 fn from(e: crate::netstack::InternalErrorKind) -> Self {
103 match e {
104 crate::netstack::InternalErrorKind::InvalidSocketState => {
105 InternalErrorKind::InvalidSocketState
106 }
107 crate::netstack::InternalErrorKind::InternalResponseMismatch => {
108 InternalErrorKind::InternalResponseMismatch
109 }
110 crate::netstack::InternalErrorKind::InternalChannelClosed => {
111 InternalErrorKind::InternalChannelClosed
112 }
113 crate::netstack::InternalErrorKind::BadListenerHandle => {
114 InternalErrorKind::BadListenerHandle
115 }
116 crate::netstack::InternalErrorKind::BadSocketHandle => {
117 InternalErrorKind::BadSocketHandle
118 }
119 crate::netstack::InternalErrorKind::BufferFull => InternalErrorKind::BufferFull,
120 _ => unreachable!(),
121 }
122 }
123}
124
125impl From<ts_runtime::Error> for Error {
126 fn from(value: ts_runtime::Error) -> Self {
127 match value.kind {
128 ts_runtime::ErrorKind::Timeout => Error::Timeout,
129 ts_runtime::ErrorKind::ActorGone
130 | ts_runtime::ErrorKind::MailboxFull
131 | ts_runtime::ErrorKind::ReplyErr => Error::Internal(InternalErrorKind::Actor),
132 ts_runtime::ErrorKind::UnsupportedInTunMode | ts_runtime::ErrorKind::TunUnavailable => {
135 Error::Internal(InternalErrorKind::UnsupportedInTunMode)
136 }
137 }
138 }
139}
140
141impl From<NetstackError> for Error {
142 fn from(value: NetstackError) -> Self {
143 match value {
144 NetstackError::Internal(k) => Error::Internal(k.into()),
145 NetstackError::ConnectionReset => Error::ConnectionReset,
146 NetstackError::BadRequest(_) => Error::Internal(InternalErrorKind::BadRequest),
147 }
148 }
149}