From 72edd75eab5fcf250135de06a1b19b4792591849 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 19 Nov 2017 17:51:14 -1000 Subject: [PATCH] add custom ExceptError --- src/error.rs | 20 +++++++++++++++++++- src/multipart.rs | 2 +- src/route.rs | 9 ++++----- src/task.rs | 1 + 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/error.rs b/src/error.rs index 939ddf438..873b819f1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,7 +16,7 @@ pub use cookie::{ParseError as CookieParseError}; use body::Body; use httpresponse::HttpResponse; -use httpcodes::{HTTPBadRequest, HTTPMethodNotAllowed}; +use httpcodes::{HTTPBadRequest, HTTPMethodNotAllowed, HTTPExpectationFailed}; /// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) /// for actix web operations @@ -259,6 +259,24 @@ impl ErrorResponse for MultipartError { } } +/// Error during handling `Expect` header +#[derive(Fail, PartialEq, Debug)] +pub enum ExpectError { + /// Expect header value can not be converted to utf8 + #[fail(display="Expect header value can not be converted to utf8")] + Encoding, + /// Unknown expect value + #[fail(display="Unknown expect value")] + UnknownExpect, +} + +impl ErrorResponse for ExpectError { + + fn error_response(&self) -> HttpResponse { + HTTPExpectationFailed.with_body("Unknown Expect") + } +} + /// Websocket handshake errors #[derive(Fail, PartialEq, Debug)] pub enum WsHandshakeError { diff --git a/src/multipart.rs b/src/multipart.rs index 64b434275..af83e3fda 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -1,4 +1,4 @@ -//! Multipart requests support. +//! Multipart requests support use std::{cmp, fmt}; use std::rc::Rc; use std::cell::RefCell; diff --git a/src/route.rs b/src/route.rs index 8f1d1cb4a..d99f0393b 100644 --- a/src/route.rs +++ b/src/route.rs @@ -8,13 +8,12 @@ use futures::Stream; use task::{Task, DrainFut}; use body::Binary; -use error::Error; +use error::{Error, ExpectError}; use context::HttpContext; use resource::Reply; use payload::Payload; use httprequest::HttpRequest; use httpresponse::HttpResponse; -use httpcodes::HTTPExpectationFailed; #[doc(hidden)] #[derive(Debug)] @@ -52,7 +51,7 @@ pub trait Route: Actor { type State; /// Handle `EXPECT` header. By default respones with `HTTP/1.1 100 Continue` - fn expect(req: &HttpRequest, ctx: &mut Self::Context) -> Result<(), HttpResponse> + fn expect(req: &HttpRequest, ctx: &mut Self::Context) -> Result<(), Error> where Self: Actor> { // handle expect header only for HTTP/1.1 @@ -63,10 +62,10 @@ pub trait Route: Actor { ctx.write("HTTP/1.1 100 Continue\r\n\r\n"); Ok(()) } else { - Err(HTTPExpectationFailed.with_body("Unknown Expect")) + Err(ExpectError::UnknownExpect.into()) } } else { - Err(HTTPExpectationFailed.with_body("Unknown Expect")) + Err(ExpectError::Encoding.into()) } } else { Ok(()) diff --git a/src/task.rs b/src/task.rs index 80ef6621d..6530443b9 100644 --- a/src/task.rs +++ b/src/task.rs @@ -61,6 +61,7 @@ pub(crate) trait IoContext: Stream + 'static { fn disconnected(&mut self); } +/// Future that resolves when all buffered data get sent #[doc(hidden)] #[derive(Debug)] pub struct DrainFut {