diff --git a/Cargo.toml b/Cargo.toml index 576ed05..362c55f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ http = { version = "0.2", optional = true } hyper = { version = "^0.14", optional = true } axum-lib = { version = "^0.2", optional = true, package = "axum" } http-body = { version = "^0.4", optional = true } -poem-lib = { version = "1.0.23", optional = true, package = "poem" } +poem-lib = { version = "1", optional = true, package = "poem" } [target."cfg(not(target_arch = \"wasm32\"))".dependencies] hostname = "^0.3" diff --git a/src/binding/poem/extractor.rs b/src/binding/poem/extractor.rs index cd2118b..b9b1396 100644 --- a/src/binding/poem/extractor.rs +++ b/src/binding/poem/extractor.rs @@ -1,43 +1,20 @@ use async_trait::async_trait; -use poem_lib::error::ReadBodyError; +use poem_lib::error::ResponseError; use poem_lib::http::StatusCode; -use poem_lib::{FromRequest, IntoResponse, Request, RequestBody, Response}; +use poem_lib::{FromRequest, Request, RequestBody, Result}; use crate::binding::http::to_event; use crate::Event; -#[derive(Debug)] -pub enum ParseEventError { - ReadBody(ReadBodyError), - ParseEvent(crate::message::Error), -} - -impl From for ParseEventError { - fn from(err: ReadBodyError) -> Self { - ParseEventError::ReadBody(err) - } -} - -impl From for ParseEventError { - fn from(err: crate::message::Error) -> Self { - ParseEventError::ParseEvent(err) - } -} - -impl IntoResponse for ParseEventError { - fn into_response(self) -> Response { - match self { - ParseEventError::ReadBody(err) => err.into_response(), - ParseEventError::ParseEvent(err) => (StatusCode::BAD_REQUEST, err.to_string()).into(), - } +impl ResponseError for crate::message::Error { + fn status(&self) -> StatusCode { + StatusCode::BAD_REQUEST } } #[async_trait] impl<'a> FromRequest<'a> for Event { - type Error = ParseEventError; - - async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result { + async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result { Ok(to_event(req.headers(), body.take()?.into_vec().await?)?) } } @@ -79,12 +56,9 @@ mod tests { .finish(); let (req, mut body) = req.split(); - let resp = Event::from_request(&req, &mut body).await.into_response(); + let resp = Event::from_request(&req, &mut body).await.err().unwrap(); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); - assert_eq!( - resp.into_body().into_string().await.unwrap(), - "Invalid specversion BAD SPECIFICATION" - ); + assert_eq!(resp.to_string(), "Invalid specversion BAD SPECIFICATION"); } #[tokio::test] diff --git a/src/binding/poem/mod.rs b/src/binding/poem/mod.rs index c26d8c0..75a8ee1 100644 --- a/src/binding/poem/mod.rs +++ b/src/binding/poem/mod.rs @@ -55,5 +55,3 @@ mod extractor; mod response; - -pub use extractor::ParseEventError;