aboutsummaryrefslogtreecommitdiff
path: root/src/request.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/request.rs')
-rw-r--r--src/request.rs24
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