diff --git a/examples/htmlx/src/main.rs b/examples/htmlx/src/main.rs index 030a5c80..3dd61a45 100644 --- a/examples/htmlx/src/main.rs +++ b/examples/htmlx/src/main.rs @@ -52,7 +52,7 @@ async fn index(req: Request) -> Result { "todos": todos }), ) - .map_err(Error::normal)?; + .map_err(Error::boxed)?; Ok(Response::html(body)) } @@ -70,7 +70,7 @@ async fn list(req: Request) -> Result { "todos": todos }), ) - .map_err(Error::normal)?; + .map_err(Error::boxed)?; Ok(Response::html(body)) } diff --git a/examples/otel/metrics/src/main.rs b/examples/otel/metrics/src/main.rs index 237ae5d6..97fe9b1e 100644 --- a/examples/otel/metrics/src/main.rs +++ b/examples/otel/metrics/src/main.rs @@ -32,7 +32,7 @@ async fn main() -> Result<()> { ExporterBuilder::default() .with_registry(registry.clone()) .build() - .map_err(Error::normal)?, + .map_err(Error::boxed)?, metrics::new_view( Instrument::new().name("http.server.duration"), Stream::new().aggregation(Aggregation::ExplicitBucketHistogram { diff --git a/examples/routing/openapi/src/main.rs b/examples/routing/openapi/src/main.rs index e91294e8..f5b8a2eb 100644 --- a/examples/routing/openapi/src/main.rs +++ b/examples/routing/openapi/src/main.rs @@ -284,7 +284,7 @@ async fn swagger_ui(req: Request) -> Result { .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_error())? { Some(file) => Ok({ - let content_type = HeaderValue::from_str(&file.content_type).map_err(Error::normal)?; + let content_type = HeaderValue::from_str(&file.content_type).map_err(Error::boxed)?; let mut resp = Response::new(Full::from(file.bytes).into()); resp.headers_mut() diff --git a/examples/sse/src/main.rs b/examples/sse/src/main.rs index 937f4379..5eeb2984 100644 --- a/examples/sse/src/main.rs +++ b/examples/sse/src/main.rs @@ -39,8 +39,8 @@ async fn stats(req: Request) -> Result { IntervalStream::new(interval(Duration::from_secs(10))).map(move |_| { match sys .load_average() - .map_err(Error::normal) - .and_then(|loadavg| serde_json::to_string(&loadavg).map_err(Error::normal)) + .map_err(Error::boxed) + .and_then(|loadavg| serde_json::to_string(&loadavg).map_err(Error::boxed)) { Ok(loadavg) => Event::default().data(loadavg), Err(err) => { diff --git a/examples/templates/askama/src/main.rs b/examples/templates/askama/src/main.rs index 99c79c7f..63cf62d8 100644 --- a/examples/templates/askama/src/main.rs +++ b/examples/templates/askama/src/main.rs @@ -18,7 +18,7 @@ async fn index(_: Request) -> Result { buf.extend( HelloTemplate { name: "world" } .render() - .map_err(Error::normal)? + .map_err(Error::boxed)? .as_bytes(), ); diff --git a/examples/templates/minijinja/src/main.rs b/examples/templates/minijinja/src/main.rs index feebd751..65d9467b 100644 --- a/examples/templates/minijinja/src/main.rs +++ b/examples/templates/minijinja/src/main.rs @@ -26,7 +26,7 @@ async fn index(_: Request) -> Result { let mut buf = BytesMut::with_capacity(512); buf.extend( TPLS.get_template("index.html") - .map_err(Error::normal)? + .map_err(Error::boxed)? .render(context! { title => "Viz.rs", users => &vec![ @@ -40,7 +40,7 @@ async fn index(_: Request) -> Result { }, ], }) - .map_err(Error::normal)? + .map_err(Error::boxed)? .as_bytes(), ); diff --git a/examples/templates/tera/src/main.rs b/examples/templates/tera/src/main.rs index 1ec3438e..dc74c56c 100644 --- a/examples/templates/tera/src/main.rs +++ b/examples/templates/tera/src/main.rs @@ -37,7 +37,7 @@ async fn index(_: Request) -> Result { let mut buf = BytesMut::with_capacity(512); buf.extend( TPLS.render("index.html", &ctx) - .map_err(Error::normal)? + .map_err(Error::boxed)? .as_bytes(), ); diff --git a/viz-core/src/body.rs b/viz-core/src/body.rs index df2103ed..4a4984e9 100644 --- a/viz-core/src/body.rs +++ b/viz-core/src/body.rs @@ -53,8 +53,8 @@ impl Body for IncomingBody { Self::Empty => Poll::Ready(None), Self::Incoming(i) => match i { None => Poll::Ready(None), - Some(b) => match Pin::new(b).poll_frame(cx)? { - Poll::Ready(Some(f)) => Poll::Ready(Some(Ok(f))), + Some(inner) => match Pin::new(inner).poll_frame(cx)? { + Poll::Ready(Some(frame)) => Poll::Ready(Some(Ok(frame))), Poll::Ready(None) => { // the body has been used. *i = None; @@ -66,6 +66,7 @@ impl Body for IncomingBody { } } + #[inline] fn is_end_stream(&self) -> bool { match self { Self::Empty | Self::Incoming(None) => true, @@ -73,6 +74,7 @@ impl Body for IncomingBody { } } + #[inline] fn size_hint(&self) -> SizeHint { match self { Self::Empty | Self::Incoming(None) => SizeHint::with_exact(0), @@ -84,11 +86,12 @@ impl Body for IncomingBody { impl Stream for IncomingBody { type Item = Result>; + #[inline] fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.get_mut() { Self::Empty | Self::Incoming(None) => Poll::Ready(None), Self::Incoming(Some(inner)) => match Pin::new(inner).poll_frame(cx)? { - Poll::Ready(Some(f)) => Poll::Ready(f.into_data().map(Ok).ok()), + Poll::Ready(Some(frame)) => Poll::Ready(frame.into_data().map(Ok).ok()), Poll::Ready(None) => Poll::Ready(None), Poll::Pending => Poll::Pending, }, @@ -160,24 +163,26 @@ impl Body for OutgoingBody { ) -> Poll, Self::Error>>> { match self.get_mut() { Self::Empty => Poll::Ready(None), - Self::Full(f) => Pin::new(f).poll_frame(cx).map_err(Error::from), - Self::Boxed(b) => Pin::new(b).poll_frame(cx), + Self::Full(full) => Pin::new(full).poll_frame(cx).map_err(Error::from), + Self::Boxed(body) => Pin::new(body).poll_frame(cx), } } + #[inline] fn is_end_stream(&self) -> bool { match self { Self::Empty => true, - Self::Full(f) => f.is_end_stream(), - Self::Boxed(b) => b.is_end_stream(), + Self::Full(full) => full.is_end_stream(), + Self::Boxed(body) => body.is_end_stream(), } } + #[inline] fn size_hint(&self) -> SizeHint { match self { Self::Empty => SizeHint::with_exact(0), - Self::Full(f) => f.size_hint(), - Self::Boxed(b) => b.size_hint(), + Self::Full(full) => full.size_hint(), + Self::Boxed(body) => body.size_hint(), } } } @@ -185,17 +190,18 @@ impl Body for OutgoingBody { impl Stream for OutgoingBody { type Item = Result; + #[inline] fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match match self.get_mut() { Self::Empty => return Poll::Ready(None), - Self::Full(f) => Pin::new(f) + Self::Full(full) => Pin::new(full) .poll_frame(cx) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?, - Self::Boxed(b) => Pin::new(b) + Self::Boxed(body) => Pin::new(body) .poll_frame(cx) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?, } { - Poll::Ready(Some(f)) => Poll::Ready(f.into_data().map(Ok).ok()), + Poll::Ready(Some(frame)) => Poll::Ready(frame.into_data().map(Ok).ok()), Poll::Ready(None) => Poll::Ready(None), Poll::Pending => Poll::Pending, } @@ -205,8 +211,8 @@ impl Stream for OutgoingBody { fn size_hint(&self) -> (usize, Option) { let sh = match self { Self::Empty => return (0, Some(0)), - Self::Full(f) => f.size_hint(), - Self::Boxed(b) => b.size_hint(), + Self::Full(full) => full.size_hint(), + Self::Boxed(body) => body.size_hint(), }; ( usize::try_from(sh.lower()).unwrap_or(usize::MAX), diff --git a/viz-core/src/error.rs b/viz-core/src/error.rs index 91667805..7ac91a3f 100644 --- a/viz-core/src/error.rs +++ b/viz-core/src/error.rs @@ -2,27 +2,30 @@ use std::error::Error as StdError; use crate::{IntoResponse, Response, StatusCode, ThisError}; +/// An owned dynamically typed [`StdError`]. +pub type BoxError = Box; + /// Represents errors that can occur handling application. #[derive(ThisError, Debug)] pub enum Error { - /// Receives a [`Response`] as error. + /// Receives a [`Response`] as an error. #[error("response")] Responder(Response), - /// Receives a boxed [`std::error::Error`][StdError] as error. + /// Receives a boxed [`std::error::Error`][StdError] as an error. #[error(transparent)] - Normal(Box), - /// Receives a boxed [`std::error::Error`][StdError] and [`Response`] pair as error. + Boxed(BoxError), + /// Receives a boxed [`std::error::Error`][StdError] and [`Response`] pair as an error. #[error("report")] - Report(Box, Response), + Report(BoxError, Response), } impl Error { /// Create a new error object from any error type. - pub fn normal(t: T) -> Self + pub fn boxed(t: T) -> Self where T: StdError + Send + Sync + 'static, { - Self::Normal(Box::new(t)) + Self::Boxed(Box::new(t)) } /// Forwards to the method defined on the type `dyn Error`. @@ -32,7 +35,7 @@ impl Error { T: StdError + 'static, { match self { - Self::Normal(e) | Self::Report(e, _) => e.is::(), + Self::Boxed(e) | Self::Report(e, _) => e.is::(), Self::Responder(_) => false, } } @@ -47,10 +50,10 @@ impl Error { where T: StdError + 'static, { - if let Self::Normal(e) = self { + if let Self::Boxed(e) = self { return match e.downcast::() { Ok(e) => Ok(*e), - Err(e) => Err(Self::Normal(e)), + Err(e) => Err(Self::Boxed(e)), }; } if let Self::Report(e, r) = self { @@ -68,7 +71,7 @@ impl Error { where T: StdError + 'static, { - if let Self::Normal(e) = self { + if let Self::Boxed(e) = self { return e.downcast_ref::(); } if let Self::Report(e, _) = self { @@ -83,7 +86,7 @@ impl Error { where T: StdError + 'static, { - if let Self::Normal(e) = self { + if let Self::Boxed(e) = self { return e.downcast_mut::(); } if let Self::Report(e, _) = self { @@ -117,18 +120,18 @@ impl From for Error { impl From for Error { fn from(e: std::convert::Infallible) -> Self { - Self::normal(e) + Self::boxed(e) } } impl From for Error { fn from(e: std::io::Error) -> Self { - Self::normal(e) + Self::boxed(e) } } -impl From> for Error { - fn from(value: Box) -> Self { - Self::Normal(value) +impl From for Error { + fn from(value: BoxError) -> Self { + Self::Boxed(value) } } diff --git a/viz-core/src/handler.rs b/viz-core/src/handler.rs index 2308300d..efe5337a 100644 --- a/viz-core/src/handler.rs +++ b/viz-core/src/handler.rs @@ -3,37 +3,51 @@ use crate::{async_trait, Future}; mod after; -mod and_then; -mod around; -mod before; -mod boxed; -mod catch_error; -mod catch_unwind; -mod either; -mod fn_ext; -mod fn_ext_hanlder; -mod into_handler; -mod map; -mod map_err; -mod map_into_response; -mod or_else; -mod transform; - pub use after::After; + +mod and_then; pub use and_then::AndThen; + +mod around; pub use around::{Around, Next}; + +mod before; pub use before::Before; + +mod boxed; pub use boxed::BoxHandler; + +mod catch_error; pub use catch_error::CatchError; + +mod catch_unwind; pub use catch_unwind::CatchUnwind; + +mod either; pub use either::Either; + +mod fn_ext; pub use fn_ext::FnExt; + +mod fn_ext_hanlder; pub use fn_ext_hanlder::FnExtHandler; + +mod into_handler; pub use into_handler::IntoHandler; + +mod map; pub use map::Map; + +mod map_err; pub use map_err::MapErr; + +mod map_into_response; pub use map_into_response::MapInToResponse; + +mod or_else; pub use or_else::OrElse; + +mod transform; pub use transform::Transform; /// A simplified asynchronous interface for handling input and output. @@ -48,8 +62,6 @@ pub trait Handler: dyn_clone::DynClone + Send + Sync + 'static { async fn call(&self, input: Input) -> Self::Output; } -impl HandlerExt for T where T: Handler {} - #[async_trait] impl Handler for F where @@ -180,3 +192,5 @@ pub trait HandlerExt: Handler { f(self) } } + +impl HandlerExt for T where T: Handler {} diff --git a/viz-core/src/into_response.rs b/viz-core/src/into_response.rs index 755c3817..616cf1a4 100644 --- a/viz-core/src/into_response.rs +++ b/viz-core/src/into_response.rs @@ -26,7 +26,7 @@ impl IntoResponse for Response { impl IntoResponse for Error { fn into_response(self) -> Response { match self { - Error::Normal(error) => { + Error::Boxed(error) => { let body = error.to_string(); Response::builder() .status(StatusCode::INTERNAL_SERVER_ERROR) diff --git a/viz-core/src/lib.rs b/viz-core/src/lib.rs index ac3c91b9..c308de5f 100644 --- a/viz-core/src/lib.rs +++ b/viz-core/src/lib.rs @@ -13,18 +13,10 @@ ))] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -/// Represents an HTTP Request. -pub type Request = http::Request; -/// Represents an HTTP Response. -pub type Response = http::Response; -/// Represents either success (Ok) or failure (Err). -pub type Result = core::result::Result; - #[macro_use] pub(crate) mod macros; pub mod handler; - #[doc(inline)] pub use crate::handler::{BoxHandler, FnExt, Handler, HandlerExt, IntoHandler, Next, Transform}; @@ -32,19 +24,30 @@ pub mod middleware; pub mod types; mod body; +pub use body::{IncomingBody, OutgoingBody}; + mod error; -mod from_request; -mod into_response; -mod request; -mod response; +pub use error::{BoxError, Error}; -pub use body::{IncomingBody, OutgoingBody}; -pub use error::Error; +mod from_request; pub use from_request::FromRequest; + +mod into_response; pub use into_response::IntoResponse; + +mod request; pub use request::RequestExt; + +mod response; pub use response::ResponseExt; +/// Represents an HTTP Request. +pub type Request = http::Request; +/// Represents an HTTP Response. +pub type Response = http::Response; +/// Represents either success (Ok) or failure (Err). +pub type Result = core::result::Result; + pub use async_trait::async_trait; pub use bytes::{Bytes, BytesMut}; #[doc(inline)] diff --git a/viz-core/tests/error.rs b/viz-core/tests/error.rs index 76535f6f..f63c9b81 100644 --- a/viz-core/tests/error.rs +++ b/viz-core/tests/error.rs @@ -6,7 +6,7 @@ fn error() { let e: Error = std::io::Error::last_os_error().into(); assert!(e.is::()); assert!(e.downcast::().is_ok()); - let e: Error = Error::normal(std::io::Error::last_os_error()); + let e: Error = Error::boxed(std::io::Error::last_os_error()); assert!(e.downcast_ref::().is_some()); let boxed: Box = Box::new(std::io::Error::last_os_error()); let mut e: Error = boxed.into(); diff --git a/viz-router/src/router.rs b/viz-router/src/router.rs index b368341c..0187092f 100644 --- a/viz-router/src/router.rs +++ b/viz-router/src/router.rs @@ -298,7 +298,7 @@ mod tests { let mut buf = bytes::BytesMut::new(); buf.extend(b"users: "); - buf.extend(body.collect().await.map_err(Error::normal)?.to_bytes()); + buf.extend(body.collect().await.map_err(Error::boxed)?.to_bytes()); Ok(Response::from_parts(parts, Full::from(buf.freeze()).into())) }) @@ -319,7 +319,7 @@ mod tests { let mut buf = bytes::BytesMut::new(); buf.extend(b"posts: "); - buf.extend(body.collect().await.map_err(Error::normal)?.to_bytes()); + buf.extend(body.collect().await.map_err(Error::boxed)?.to_bytes()); Ok(Response::from_parts(parts, Full::from(buf.freeze()).into())) }) diff --git a/viz-test/src/lib.rs b/viz-test/src/lib.rs index 7267bd22..b48e7b10 100644 --- a/viz-test/src/lib.rs +++ b/viz-test/src/lib.rs @@ -27,7 +27,7 @@ impl TestServer { let client = reqwest::Client::builder() .redirect(reqwest::redirect::Policy::none()) .build() - .map_err(Error::normal)?; + .map_err(Error::boxed)?; tokio::spawn(run(listener, tree)); diff --git a/viz-test/tests/body.rs b/viz-test/tests/body.rs index ffed2441..53d03936 100644 --- a/viz-test/tests/body.rs +++ b/viz-test/tests/body.rs @@ -52,16 +52,16 @@ async fn incoming_body() -> Result<()> { .post("/login-empty") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, ""); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, ""); let resp = client .post("/login") .body("hello world!") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, ""); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, ""); Ok(()) } @@ -118,16 +118,16 @@ async fn incoming_stream() -> Result<()> { .post("/login-empty") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, ""); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, ""); let resp = client .post("/login") .body("hello world!") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, ""); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, ""); Ok(()) } diff --git a/viz-test/tests/payload.rs b/viz-test/tests/payload.rs index 2161abd4..a9350e51 100644 --- a/viz-test/tests/payload.rs +++ b/viz-test/tests/payload.rs @@ -41,10 +41,10 @@ async fn payload() -> Result<()> { .json(&form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "unsupported media type, `application/x-www-form-urlencoded` is required" ); let resp = client @@ -52,10 +52,10 @@ async fn payload() -> Result<()> { .form(&form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "payload is too large" ); @@ -65,10 +65,10 @@ async fn payload() -> Result<()> { .form(&form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "unsupported media type, `application/javascript; charset=utf-8` is required" ); let resp = client @@ -76,10 +76,10 @@ async fn payload() -> Result<()> { .json(&form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "payload is too large" ); @@ -89,10 +89,10 @@ async fn payload() -> Result<()> { .json(&form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "unsupported media type, `multipart/form-data` is required" ); let form = viz_test::multipart::Form::new() @@ -103,10 +103,10 @@ async fn payload() -> Result<()> { .multipart(form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "payload is too large" ); diff --git a/viz-test/tests/request.rs b/viz-test/tests/request.rs index 4c80d193..0d6d24bc 100644 --- a/viz-test/tests/request.rs +++ b/viz-test/tests/request.rs @@ -94,7 +94,7 @@ async fn request_body() -> Result<()> { while let Some(mut field) = multipart.try_next().await? { let buf = field.bytes().await?.to_vec(); - data.insert(field.name, String::from_utf8(buf).map_err(Error::normal)?); + data.insert(field.name, String::from_utf8(buf).map_err(Error::boxed)?); } Ok(Response::json(data)) @@ -103,23 +103,23 @@ async fn request_body() -> Result<()> { let client = TestServer::new(router).await?; - let resp = client.get("/7").send().await.map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "7"); + let resp = client.get("/7").send().await.map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "7"); let resp = client .get("/viz-rs/viz") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "viz-rs/viz"); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "viz-rs/viz"); let resp = client .get("/extract-token") .header(AUTHORIZATION, "Bearer viz.rs") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "viz.rs"); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "viz.rs"); let mut form = BTreeMap::new(); form.insert("password", "rs"); @@ -129,9 +129,9 @@ async fn request_body() -> Result<()> { .form(&form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, r#"{"password":"rs","username":"viz"}"# ); @@ -140,43 +140,43 @@ async fn request_body() -> Result<()> { .header(COOKIE, "viz=crate") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "crate"); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "crate"); let resp = client .get("/cookies") .header(COOKIE, "auth=true;dark=false") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "2"); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "2"); let resp = client .post("/bytes") .body("bytes") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "bytes"); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "bytes"); let resp = client .post("/bytes-with-limit") .body("rust") .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::OK); - assert_eq!(resp.text().await.map_err(Error::normal)?, "rust"); + assert_eq!(resp.text().await.map_err(Error::boxed)?, "rust"); let resp = client .post("/bytes-with-limit") .body("crate") .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "payload is too large" ); @@ -185,10 +185,10 @@ async fn request_body() -> Result<()> { .body("used") .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); assert_eq!( - resp.text().await.map_err(Error::normal)?, + resp.text().await.map_err(Error::boxed)?, "payload has been used" ); @@ -197,17 +197,17 @@ async fn request_body() -> Result<()> { .body("text") .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "text"); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "text"); let resp = client .post("/json") .json(&Page { p: 1 }) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!( - resp.json::().await.map_err(Error::normal)?, + resp.json::().await.map_err(Error::boxed)?, Page { p: 1 } ); @@ -219,11 +219,11 @@ async fn request_body() -> Result<()> { .form(&form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!( resp.json::>() .await - .map_err(Error::normal)?, + .map_err(Error::boxed)?, { let mut form = HashMap::new(); form.insert("username".to_string(), "viz".to_string()); @@ -240,11 +240,11 @@ async fn request_body() -> Result<()> { .multipart(form) .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; assert_eq!( resp.json::>() .await - .map_err(Error::normal)?, + .map_err(Error::boxed)?, { let mut form = HashMap::new(); form.insert("key3".to_string(), "3".to_string()); @@ -287,17 +287,17 @@ async fn request_session() -> Result<()> { .post("/session/set") .send() .await - .map_err(Error::normal)?; + .map_err(Error::boxed)?; let cookie = resp.headers().get(SET_COOKIE).cloned().unwrap(); - assert_eq!(resp.text().await.map_err(Error::normal)?, "1"); + assert_eq!(resp.text().await.map_err(Error::boxed)?, "1"); let resp = client .post("/session/set") .header(COOKIE, cookie) .send() .await - .map_err(Error::normal)?; - assert_eq!(resp.text().await.map_err(Error::normal)?, "2"); + .map_err(Error::boxed)?; + assert_eq!(resp.text().await.map_err(Error::boxed)?, "2"); Ok(()) } diff --git a/viz-test/tests/response.rs b/viz-test/tests/response.rs index 0a67b9e8..242738dd 100644 --- a/viz-test/tests/response.rs +++ b/viz-test/tests/response.rs @@ -140,13 +140,13 @@ async fn response_ext_with_server() -> Result<()> { let client = TestServer::new(router).await?; - let resp = client.get("/").send().await.map_err(Error::normal)?; + let resp = client.get("/").send().await.map_err(Error::boxed)?; assert_eq!(resp.content_length(), Some(0)); - assert_eq!(resp.text().await.map_err(Error::normal)?, ""); + assert_eq!(resp.text().await.map_err(Error::boxed)?, ""); - let resp = client.post("/").send().await.map_err(Error::normal)?; + let resp = client.post("/").send().await.map_err(Error::boxed)?; assert_eq!(resp.content_length(), Some(6)); - assert_eq!(resp.text().await.map_err(Error::normal)?, ""); + assert_eq!(resp.text().await.map_err(Error::boxed)?, ""); Ok(()) } diff --git a/viz/src/tls/rustls.rs b/viz/src/tls/rustls.rs index bffa3c02..7540e7c5 100644 --- a/viz/src/tls/rustls.rs +++ b/viz/src/tls/rustls.rs @@ -98,27 +98,27 @@ impl Config { pub fn build(self) -> Result { fn read_trust_anchor(trust_anchor: &Certificate) -> Result { let mut store = RootCertStore::empty(); - store.add(trust_anchor).map_err(Error::normal)?; + store.add(trust_anchor).map_err(Error::boxed)?; Ok(store) } let certs = rustls_pemfile::certs(&mut self.cert.as_slice()) .map(|mut certs| certs.drain(..).map(Certificate).collect()) - .map_err(Error::normal)?; + .map_err(Error::boxed)?; let keys = { let mut pkcs8: Vec = rustls_pemfile::pkcs8_private_keys(&mut self.key.as_slice()) .map(|mut keys| keys.drain(..).map(PrivateKey).collect()) - .map_err(Error::normal)?; + .map_err(Error::boxed)?; if pkcs8.is_empty() { let mut rsa: Vec = rustls_pemfile::rsa_private_keys(&mut self.key.as_slice()) .map(|mut keys| keys.drain(..).map(PrivateKey).collect()) - .map_err(Error::normal)?; + .map_err(Error::boxed)?; if rsa.is_empty() { - return Err(Error::normal(IoError::new( + return Err(Error::boxed(IoError::new( ErrorKind::InvalidData, "failed to parse tls private keys", ))); @@ -145,7 +145,7 @@ impl Config { .with_safe_defaults() .with_client_cert_verifier(client_auth) .with_single_cert_with_ocsp_and_sct(certs, keys, self.ocsp_resp, Vec::new()) - .map_err(Error::normal) + .map_err(Error::boxed) } }