use std::fmt;
use crate::remote_error::RemoteError;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TransportErrorKind {
Connect,
Protocol,
Http { status: u16 },
Closed,
Other,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransportError {
pub kind: TransportErrorKind,
pub message: String,
}
impl TransportError {
pub fn new(kind: TransportErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
}
}
pub fn connect(message: impl Into<String>) -> Self {
Self::new(TransportErrorKind::Connect, message)
}
pub fn protocol(message: impl Into<String>) -> Self {
Self::new(TransportErrorKind::Protocol, message)
}
pub fn http(status: u16, message: impl Into<String>) -> Self {
Self::new(TransportErrorKind::Http { status }, message)
}
pub fn closed(message: impl Into<String>) -> Self {
Self::new(TransportErrorKind::Closed, message)
}
pub fn other(message: impl Into<String>) -> Self {
Self::new(TransportErrorKind::Other, message)
}
}
impl fmt::Display for TransportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
TransportErrorKind::Connect => write!(f, "transport connect: {}", self.message),
TransportErrorKind::Protocol => write!(f, "transport protocol: {}", self.message),
TransportErrorKind::Http { status } => {
write!(f, "transport http {}: {}", status, self.message)
}
TransportErrorKind::Closed => write!(f, "transport closed: {}", self.message),
TransportErrorKind::Other => write!(f, "transport: {}", self.message),
}
}
}
impl std::error::Error for TransportError {}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ClientError {
Transport(TransportError),
Remote(RemoteError),
Decode {
content_type: Option<String>,
message: String,
},
Encode {
param: &'static str,
message: String,
},
InvalidName {
name: String,
reason: NameRefusal,
},
ShapeMismatch {
path: String,
wanted: String,
got: String,
methods: Vec<String>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NameRefusal {
Empty,
Dotted,
NotASegment,
StaticChild,
}
impl fmt::Display for NameRefusal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
NameRefusal::Empty => "it is empty",
NameRefusal::Dotted => "it contains '.', which the service reads as two segments",
NameRefusal::StaticChild => "it is a static child's segment, which the service routes there",
NameRefusal::NotASegment => "it is not a route segment ([a-z][a-z0-9_]*), which the service refuses",
})
}
}
impl ClientError {
pub fn is_transport(&self) -> bool {
matches!(self, ClientError::Transport(_))
}
pub fn is_remote(&self) -> bool {
matches!(self, ClientError::Remote(_))
}
pub fn remote(&self) -> Option<&RemoteError> {
match self {
ClientError::Remote(e) => Some(e),
_ => None,
}
}
}
impl From<TransportError> for ClientError {
fn from(e: TransportError) -> Self {
ClientError::Transport(e)
}
}
impl From<RemoteError> for ClientError {
fn from(e: RemoteError) -> Self {
ClientError::Remote(e)
}
}
impl fmt::Display for ClientError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ClientError::Transport(e) => write!(f, "{e}"),
ClientError::Remote(crate::RemoteError::Execution { message }) => write!(f, "remote error: {message}"),
ClientError::Remote(e) => write!(f, "remote error: {e}"),
ClientError::Decode {
content_type: Some(ct),
message,
} => write!(f, "decode {ct}: {message}"),
ClientError::Decode { message, .. } => write!(f, "decode: {message}"),
ClientError::Encode { param, message } => {
write!(f, "encode param `{param}`: {message}")
}
ClientError::InvalidName { name, reason } => {
write!(f, "instance name {name:?} refused, nothing sent: {reason}")
}
ClientError::ShapeMismatch { path, wanted, got, methods } => write!(
f,
"`{path}` does not serve the interface wanted ({wanted}, it serves {got}): {}",
methods.join(", ")
),
}
}
}
impl std::error::Error for ClientError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ClientError::Transport(e) => Some(e),
ClientError::Remote(e) => Some(e),
ClientError::Decode { .. }
| ClientError::Encode { .. }
| ClientError::InvalidName { .. }
| ClientError::ShapeMismatch { .. } => None,
}
}
}