From 2e1071b3943e2379bdc56b5b0838194ffe9c7530 Mon Sep 17 00:00:00 2001 From: Thomas Heck Date: Fri, 1 Sep 2023 18:56:42 +0200 Subject: [PATCH] feat: impl std::error::Error for ProxyError --- src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ef33424..f69d161 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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 for ProxyError { fn from(err: Error) -> ProxyError { ProxyError::HyperError(err)