1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

add custom ExceptError

This commit is contained in:
Nikolay Kim 2017-11-19 17:51:14 -10:00
parent 78d8d21196
commit 72edd75eab
4 changed files with 25 additions and 7 deletions

View File

@ -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 {

View File

@ -1,4 +1,4 @@
//! Multipart requests support.
//! Multipart requests support
use std::{cmp, fmt};
use std::rc::Rc;
use std::cell::RefCell;

View File

@ -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<Context=HttpContext<Self>>
{
// 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(())

View File

@ -61,6 +61,7 @@ pub(crate) trait IoContext: Stream<Item=Frame, Error=Error> + 'static {
fn disconnected(&mut self);
}
/// Future that resolves when all buffered data get sent
#[doc(hidden)]
#[derive(Debug)]
pub struct DrainFut {