Forward upstream errors

This commit is contained in:
asonix 2019-09-12 20:17:29 -05:00
parent 0fd087ced5
commit 3a502055bf

View file

@ -18,10 +18,6 @@ use super::{DigestPart, DigestVerify};
pub struct VerifyDigest<T>(bool, T);
pub struct VerifyMiddleware<T, S>(Rc<RefCell<S>>, bool, T);
#[derive(Debug, Fail)]
#[fail(display = "Error in upstream middleware")]
pub struct UpstreamError;
#[derive(Debug, Fail)]
#[fail(display = "Error verifying digest")]
pub struct VerifyError;
@ -42,7 +38,11 @@ where
impl<T, S> Transform<S> for VerifyDigest<T>
where
T: DigestVerify + Clone + 'static,
S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>> + 'static,
S: Service<
Request = ServiceRequest,
Response = ServiceResponse<Body>,
Error = actix_web::Error,
> + 'static,
S::Error: 'static,
{
type Request = ServiceRequest;
@ -64,7 +64,11 @@ where
impl<T, S> Service for VerifyMiddleware<T, S>
where
T: DigestVerify + Clone + 'static,
S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>> + 'static,
S: Service<
Request = ServiceRequest,
Response = ServiceResponse<Body>,
Error = actix_web::Error,
> + 'static,
S::Error: 'static,
{
type Request = ServiceRequest;
@ -73,10 +77,7 @@ where
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.0
.borrow_mut()
.poll_ready()
.map_err(|_| UpstreamError.into())
self.0.borrow_mut().poll_ready()
}
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
@ -97,12 +98,7 @@ where
.into(),
);
Either::A(
service
.borrow_mut()
.call(req)
.map_err(|_| UpstreamError.into()),
)
Either::A(service.borrow_mut().call(req))
} else {
Either::B(err(VerifyError.into()))
}
@ -111,12 +107,7 @@ where
if self.1 {
Box::new(err(VerifyError.into()))
} else {
Box::new(
self.0
.borrow_mut()
.call(req)
.map_err(|_| UpstreamError.into()),
)
Box::new(self.0.borrow_mut().call(req))
}
}
}
@ -144,16 +135,6 @@ fn parse_digest(h: &HeaderValue) -> Option<Vec<DigestPart>> {
}
}
impl ResponseError for UpstreamError {
fn error_response(&self) -> HttpResponse {
HttpResponse::InternalServerError().finish()
}
fn render_response(&self) -> HttpResponse {
Self::error_response(self)
}
}
impl ResponseError for VerifyError {
fn error_response(&self) -> HttpResponse {
HttpResponse::BadRequest().finish()