Skip to content

Commit

Permalink
feat: impl std::error::Error for ProxyError
Browse files Browse the repository at this point in the history
  • Loading branch information
chpio committed Sep 2, 2023
1 parent e73a766 commit 2e1071b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use hyper::http::uri::InvalidUri;
use hyper::upgrade::OnUpgrade;
use hyper::{Body, Client, Error, Request, Response, StatusCode};
use lazy_static::lazy_static;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::net::IpAddr;
use tokio::io::copy_bidirectional;

Expand Down Expand Up @@ -42,6 +44,28 @@ pub enum ProxyError {
UpgradeError(String),
}

impl Display for ProxyError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
ProxyError::InvalidUri(err) => write!(f, "InvalidUri: {err}"),
ProxyError::HyperError(err) => write!(f, "HyperError: {err}"),
ProxyError::ForwardHeaderError => write!(f, "ForwardHeaderError"),
ProxyError::UpgradeError(err) => write!(f, "UpgradeError: {err}"),
}
}
}

impl StdError for ProxyError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
ProxyError::InvalidUri(err) => err.source(),
ProxyError::HyperError(err) => err.source(),
ProxyError::ForwardHeaderError => None,
ProxyError::UpgradeError(..) => None,
}
}
}

impl From<Error> for ProxyError {
fn from(err: Error) -> ProxyError {
ProxyError::HyperError(err)
Expand Down

0 comments on commit 2e1071b

Please sign in to comment.