feat: impl std::error::Error for ProxyError

This commit is contained in:
Thomas Heck 2023-09-01 18:56:42 +02:00
parent e73a76600c
commit 2e1071b394
No known key found for this signature in database
GPG Key ID: 98CE419A1BD90158

View File

@ -9,6 +9,8 @@ use hyper::http::uri::InvalidUri;
use hyper::upgrade::OnUpgrade; use hyper::upgrade::OnUpgrade;
use hyper::{Body, Client, Error, Request, Response, StatusCode}; use hyper::{Body, Client, Error, Request, Response, StatusCode};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::net::IpAddr; use std::net::IpAddr;
use tokio::io::copy_bidirectional; use tokio::io::copy_bidirectional;
@ -42,6 +44,28 @@ pub enum ProxyError {
UpgradeError(String), 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 { impl From<Error> for ProxyError {
fn from(err: Error) -> ProxyError { fn from(err: Error) -> ProxyError {
ProxyError::HyperError(err) ProxyError::HyperError(err)