use std::fmt;
use crate::netstack::Error as NetstackError;
#[derive(Debug, thiserror::Error, Clone, Copy, Eq, PartialEq)]
pub enum Error {
#[error("operation timed-out")]
Timeout,
#[error("connection reset")]
ConnectionReset,
#[error("an error reading or parsing the key file")]
KeyFileRead,
#[error("an error writing out the key file")]
KeyFileWrite,
#[error("the environment variable `{}` was not set", crate::ENV_MAGIC_VAR)]
UnstableEnvVar,
#[error("internal error ({0})")]
Internal(InternalErrorKind),
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum InternalErrorKind {
InvalidSocketState,
InternalResponseMismatch,
InternalChannelClosed,
BadListenerHandle,
BadSocketHandle,
BadRequest,
BufferFull,
Actor,
}
impl fmt::Display for InternalErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InternalErrorKind::InvalidSocketState => write!(f, "invalid socket state"),
InternalErrorKind::InternalResponseMismatch => {
write!(f, "response type mismatched to request type")
}
InternalErrorKind::InternalChannelClosed => write!(f, "channel closed"),
InternalErrorKind::BadListenerHandle => write!(f, "handle to invalid TCP listener"),
InternalErrorKind::BadSocketHandle => write!(f, "handle to invalid socket"),
InternalErrorKind::BadRequest => write!(f, "bad request"),
InternalErrorKind::BufferFull => write!(f, "buffer full"),
InternalErrorKind::Actor => write!(f, "actor missing or shutdown"),
}
}
}
impl From<crate::netstack::InternalErrorKind> for InternalErrorKind {
fn from(e: crate::netstack::InternalErrorKind) -> Self {
match e {
crate::netstack::InternalErrorKind::InvalidSocketState => {
InternalErrorKind::InvalidSocketState
}
crate::netstack::InternalErrorKind::InternalResponseMismatch => {
InternalErrorKind::InternalResponseMismatch
}
crate::netstack::InternalErrorKind::InternalChannelClosed => {
InternalErrorKind::InternalChannelClosed
}
crate::netstack::InternalErrorKind::BadListenerHandle => {
InternalErrorKind::BadListenerHandle
}
crate::netstack::InternalErrorKind::BadSocketHandle => {
InternalErrorKind::BadSocketHandle
}
crate::netstack::InternalErrorKind::BufferFull => InternalErrorKind::BufferFull,
_ => unreachable!(),
}
}
}
impl From<ts_runtime::Error> for Error {
fn from(value: ts_runtime::Error) -> Self {
match value.kind {
ts_runtime::ErrorKind::Timeout => Error::Timeout,
ts_runtime::ErrorKind::ActorGone
| ts_runtime::ErrorKind::MailboxFull
| ts_runtime::ErrorKind::ReplyErr => Error::Internal(InternalErrorKind::Actor),
}
}
}
impl From<NetstackError> for Error {
fn from(value: NetstackError) -> Self {
match value {
NetstackError::Internal(k) => Error::Internal(k.into()),
NetstackError::ConnectionReset => Error::ConnectionReset,
NetstackError::BadRequest(_) => Error::Internal(InternalErrorKind::BadRequest),
}
}
}