aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/error.rs69
-rw-r--r--src/lib.rs2
-rw-r--r--src/request.rs24
3 files changed, 6 insertions, 89 deletions
diff --git a/src/error.rs b/src/error.rs
deleted file mode 100644
index fe998d0..0000000
--- a/src/error.rs
+++ /dev/null
@@ -1,69 +0,0 @@
-//! Provides the [`crate::Error`] type.
-
-use thiserror::Error;
-use hyper::StatusCode;
-
-/// Encapsulates a status code and an error message.
-#[derive(Error, Debug)]
-#[error("SimpleError({code}, {message})")]
-pub struct SimpleError {
- pub code: StatusCode,
- pub message: String,
-}
-
-impl SimpleError {
- /// Returns an HTTP response builder with the status code set to `self.code`.
- pub fn response_builder(&self) -> hyper::http::response::Builder {
- hyper::Response::builder().status(self.code)
- }
-}
-
-/// Error type for request handlers.
-///
-/// All client errors in [`crate::request`] implement [`Into<Error::Simple>`].
-#[derive(Error, Debug)]
-pub enum Error {
- #[error("SimpleError({}, {})", .0.code, .0.message)]
- Simple(#[from] SimpleError),
-
- #[error("ResponseError({})", .0.status())]
- Response(hyper::Response<hyper::Body>),
-}
-
-impl Error {
- // some convenience methods
-
- pub fn simple(code: StatusCode, message: String) -> Self {
- Error::Simple(SimpleError{code, message})
- }
-
- pub fn bad_request(message: String) -> Self {
- Error::Simple(SimpleError{code: StatusCode::BAD_REQUEST, message})
- }
-
- pub fn not_found(message: String) -> Self {
- Error::Simple(SimpleError{code: StatusCode::NOT_FOUND, message})
- }
-
- pub fn unauthorized(message: String) -> Self {
- Error::Simple(SimpleError{code: StatusCode::UNAUTHORIZED, message})
- }
-
- pub fn internal(message: String) -> Self {
- Error::Simple(SimpleError{code: StatusCode::INTERNAL_SERVER_ERROR, message})
- }
-
- pub fn method_not_allowed(message: String) -> Self {
- Error::Simple(SimpleError{code: StatusCode::METHOD_NOT_ALLOWED, message})
- }
-}
-
-macro_rules! impl_into_error_simple {
- ($type:ident, $status:expr) => {
- impl From<$type> for crate::error::Error {
- fn from(err: $type) -> Self {
- Self::Simple(crate::error::SimpleError{code: $status, message: format!("{}", err)})
- }
- }
- };
-} \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
index 391eb2c..0841e0b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,10 +1,8 @@
//! A lightweight layer on top of [Hyper](https://hyper.rs/)
//! to facilitate building web applications.
-pub use error::Error;
pub use mime;
pub use httpdate;
-#[macro_use] pub mod error;
pub mod security;
pub mod request;
pub mod response;
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