use crate::{Error as ApiError, ErrorCode};
use std::error::Error as StdError;
use std::fmt;
#[derive(Debug)]
pub enum Error {
FFIError(ApiError),
Error(http::Error),
Other(Box<dyn StdError + Send + Sync>),
InvalidHttpVersion(u32),
}
impl From<ApiError> for Error {
fn from(err: ApiError) -> Self {
Self::FFIError(err)
}
}
impl From<ErrorCode> for Error {
fn from(err: ErrorCode) -> Self {
Self::FFIError(err.into())
}
}
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
unreachable!("Something infallible failed - this should never happen.")
}
}
impl Error {
pub(super) fn from_specific(err: impl Into<http::Error>) -> Self {
Self::Error(err.into())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FFIError(e) => write!(f, "Error during FFI call: {e:}",),
Self::Error(e) => write!(f, "HTTP error: {e:}",),
Self::InvalidHttpVersion(v) => write!(f, "Invalid HTTP version: {v}",),
Self::Other(e) => write!(f, "Protocol error: {e:}",),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::FFIError(e) => Some(e),
Self::Error(e) => Some(e),
Self::Other(e) => Some(e.as_ref()),
Self::InvalidHttpVersion(_) => None,
}
}
}