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