diff options
author | Martin Fischer <martin@push-f.com> | 2021-01-22 21:34:35 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-01-22 21:36:53 +0100 |
commit | 4ba2d050bdf1a3c0070f3aa2331c82745611af1f (patch) | |
tree | 710b2d96dba397da5d5888f5099402f6271dc9fd /src/request.rs | |
parent | b886de1afc0b90d7ca27db9d5c7dabddbe3d7ee0 (diff) |
completely ditch Sputnik's general Error type
Users also want to short-circuit error types from other crates but they
cannot define a From conversion between two foreign types.
Sputnik's error type also didn't allow for proper error logging.
bump version to 0.2.3
Diffstat (limited to 'src/request.rs')
-rw-r--r-- | src/request.rs | 24 |
1 files changed, 6 insertions, 18 deletions
diff --git a/src/request.rs b/src/request.rs index c874ab5..953e7ec 100644 --- a/src/request.rs +++ b/src/request.rs @@ -93,13 +93,13 @@ impl Body { /// /// ``` /// use hyper::{Response}; - /// use sputnik::{request::Body, Error}; + /// use sputnik::request::{Body, error::FormError}; /// use serde::Deserialize; /// /// #[derive(Deserialize)] /// struct Message {text: String, year: i64} /// - /// async fn greet(body: Body) -> Result<Response<hyper::Body>, Error> { + /// async fn greet(body: Body) -> Result<Response<hyper::Body>, FormError> { /// let msg: Message = body.into_form().await?; /// Ok(Response::new(format!("hello {}", msg.text).into())) /// } @@ -120,25 +120,18 @@ impl Body { /// /// ``` /// use hyper::{Method}; - /// use sputnik::{request::{Parts, Body}, response::Response, Error}; + /// use sputnik::{request::{Parts, Body, error::CsrfProtectedFormError}, response::Response}; /// use sputnik::security::CsrfToken; /// use serde::Deserialize; /// /// #[derive(Deserialize)] /// struct Message {text: String} /// - /// async fn greet(req: &mut Parts, body: Body) -> Result<Response, Error> { + /// async fn greet(req: &mut Parts, body: Body) -> Result<Response, CsrfProtectedFormError> { /// let mut response = Response::new(); /// let csrf_token = CsrfToken::from_parts(req, &mut response); - /// *response.body() = match (req.method()) { - /// &Method::GET => format!("<form method=post> - /// <input name=text>{}<button>Submit</button></form>", csrf_token.html_input()).into(), - /// &Method::POST => { - /// let msg: Message = body.into_form_csrf(&csrf_token).await?; - /// format!("hello {}", msg.text).into() - /// }, - /// _ => return Err(Error::method_not_allowed("only GET and POST allowed".to_owned())), - /// }; + /// let msg: Message = body.into_form_csrf(&csrf_token).await?; + /// *response.body() = format!("hello {}", msg.text).into(); /// Ok(response) /// } /// ``` @@ -163,18 +156,15 @@ impl Body { pub mod error { use mime::Mime; use thiserror::Error; - use hyper::StatusCode; use crate::security::CsrfError; #[derive(Error, Debug)] #[error("query deserialize error: {0}")] pub struct QueryError(pub serde_urlencoded::de::Error); - impl_into_error_simple!(QueryError, StatusCode::BAD_REQUEST); #[derive(Error, Debug)] #[error("failed to read body")] pub struct BodyError(pub hyper::Error); - impl_into_error_simple!(BodyError, StatusCode::BAD_REQUEST); #[derive(Error, Debug)] #[error("expected Content-Type {expected} but received {}", received.as_ref().unwrap_or(&"nothing".to_owned()))] @@ -194,7 +184,6 @@ pub mod error { #[error("form deserialize error: {0}")] Deserialize(#[from] serde_urlencoded::de::Error), } - impl_into_error_simple!(FormError, StatusCode::BAD_REQUEST); #[derive(Error, Debug)] pub enum CsrfProtectedFormError { @@ -213,5 +202,4 @@ pub mod error { #[error("{0}")] Csrf(#[from] CsrfError), } - impl_into_error_simple!(CsrfProtectedFormError, StatusCode::BAD_REQUEST); }
\ No newline at end of file |