2017-11-20 04:26:31 +01:00
|
|
|
//! Error and Result module
|
2018-12-20 03:34:56 +01:00
|
|
|
use std::cell::RefCell;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::io::Error as IoError;
|
2017-10-07 06:48:14 +02:00
|
|
|
use std::str::Utf8Error;
|
|
|
|
use std::string::FromUtf8Error;
|
2018-06-19 21:27:41 +02:00
|
|
|
use std::{fmt, io, result};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
// use actix::MailboxError;
|
2018-12-20 03:34:56 +01:00
|
|
|
use backtrace::Backtrace;
|
2018-04-14 01:02:01 +02:00
|
|
|
use cookie;
|
2018-12-20 03:34:56 +01:00
|
|
|
use derive_more::{Display, From};
|
2017-12-21 05:30:54 +01:00
|
|
|
use futures::Canceled;
|
2018-03-23 02:08:12 +01:00
|
|
|
use http::uri::InvalidUri;
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::{header, Error as HttpError, StatusCode};
|
|
|
|
use httparse;
|
2018-03-27 03:18:38 +02:00
|
|
|
use serde::de::value::Error as DeError;
|
2017-11-24 19:03:13 +01:00
|
|
|
use serde_json::error::Error as JsonError;
|
2018-06-12 18:30:00 +02:00
|
|
|
use serde_urlencoded::ser::Error as FormError;
|
2018-05-25 06:03:16 +02:00
|
|
|
use tokio_timer::Error as TimerError;
|
2017-11-16 07:06:28 +01:00
|
|
|
|
|
|
|
// re-exports
|
2018-04-14 01:02:01 +02:00
|
|
|
pub use cookie::ParseError as CookieParseError;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-12-06 23:32:52 +01:00
|
|
|
use crate::body::Body;
|
2018-12-20 03:34:56 +01:00
|
|
|
use crate::response::Response;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-16 07:06:28 +01:00
|
|
|
/// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html)
|
2017-11-20 04:26:31 +01:00
|
|
|
/// for actix web operations
|
2017-11-16 07:06:28 +01:00
|
|
|
///
|
2018-04-14 01:02:01 +02:00
|
|
|
/// This typedef is generally used to avoid writing out
|
2018-10-05 06:14:18 +02:00
|
|
|
/// `actix_http::error::Error` directly and is otherwise a direct mapping to
|
2018-04-14 01:02:01 +02:00
|
|
|
/// `Result`.
|
|
|
|
pub type Result<T, E = Error> = result::Result<T, E>;
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2018-06-07 18:53:27 +02:00
|
|
|
/// General purpose actix web error.
|
|
|
|
///
|
2018-06-08 04:46:46 +02:00
|
|
|
/// An actix web error is used to carry errors from `failure` or `std::error`
|
2018-07-14 00:05:07 +02:00
|
|
|
/// through actix in a convenient way. It can be created through
|
2018-06-07 18:53:27 +02:00
|
|
|
/// converting errors with `into()`.
|
|
|
|
///
|
|
|
|
/// Whenever it is created from an external object a response error is created
|
|
|
|
/// for it that can be used to create an http response from it this means that
|
|
|
|
/// if you have access to an actix `Error` you can always get a
|
|
|
|
/// `ResponseError` reference from it.
|
2017-11-16 07:06:28 +01:00
|
|
|
pub struct Error {
|
2017-12-02 19:17:15 +01:00
|
|
|
cause: Box<ResponseError>,
|
2018-01-21 05:12:24 +01:00
|
|
|
backtrace: Option<Backtrace>,
|
2017-11-16 07:06:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 04:26:31 +01:00
|
|
|
impl Error {
|
2018-06-07 18:53:27 +02:00
|
|
|
/// Returns the reference to the underlying `ResponseError`.
|
|
|
|
pub fn as_response_error(&self) -> &ResponseError {
|
|
|
|
self.cause.as_ref()
|
|
|
|
}
|
|
|
|
|
2018-04-29 23:21:50 +02:00
|
|
|
/// Returns a reference to the Backtrace carried by this error, if it
|
|
|
|
/// carries one.
|
2018-06-07 18:53:27 +02:00
|
|
|
///
|
|
|
|
/// This uses the same `Backtrace` type that `failure` uses.
|
2018-05-04 01:26:42 +02:00
|
|
|
pub fn backtrace(&self) -> &Backtrace {
|
2018-04-29 23:21:50 +02:00
|
|
|
if let Some(bt) = self.cause.backtrace() {
|
2018-05-04 01:26:42 +02:00
|
|
|
bt
|
2018-04-29 23:21:50 +02:00
|
|
|
} else {
|
2018-05-04 01:26:42 +02:00
|
|
|
self.backtrace.as_ref().unwrap()
|
2018-04-29 23:21:50 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-07 18:53:27 +02:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// /// Attempts to downcast this `Error` to a particular `Fail` type by
|
|
|
|
// /// reference.
|
|
|
|
// ///
|
|
|
|
// /// If the underlying error is not of type `T`, this will return `None`.
|
|
|
|
// pub fn downcast_ref<T: Fail>(&self) -> Option<&T> {
|
|
|
|
// // in the most trivial way the cause is directly of the requested type.
|
|
|
|
// if let Some(rv) = Fail::downcast_ref(self.cause.as_fail()) {
|
|
|
|
// return Some(rv);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // in the more complex case the error has been constructed from a failure
|
|
|
|
// // error. This happens because we implement From<failure::Error> by
|
|
|
|
// // calling compat() and then storing it here. In failure this is
|
|
|
|
// // represented by a failure::Error being wrapped in a failure::Compat.
|
|
|
|
// //
|
|
|
|
// // So we first downcast into that compat, to then further downcast through
|
|
|
|
// // the failure's Error downcasting system into the original failure.
|
|
|
|
// let compat: Option<&failure::Compat<failure::Error>> =
|
|
|
|
// Fail::downcast_ref(self.cause.as_fail());
|
|
|
|
// compat.and_then(|e| e.get_ref().downcast_ref())
|
|
|
|
// }
|
2018-10-30 19:46:44 +01:00
|
|
|
|
|
|
|
/// Converts error to a response instance and set error message as response body
|
2018-11-18 22:48:42 +01:00
|
|
|
pub fn response_with_message(self) -> Response {
|
2018-10-30 19:46:44 +01:00
|
|
|
let message = format!("{}", self);
|
2018-11-18 05:21:28 +01:00
|
|
|
let resp: Response = self.into();
|
2018-11-18 22:48:42 +01:00
|
|
|
resp.set_body(Body::from(message))
|
2018-10-30 19:46:44 +01:00
|
|
|
}
|
2018-06-07 18:53:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// /// Helper trait to downcast a response error into a fail.
|
|
|
|
// ///
|
|
|
|
// /// This is currently not exposed because it's unclear if this is the best way
|
|
|
|
// /// to achieve the downcasting on `Error` for which this is needed.
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// pub trait InternalResponseErrorAsFail {
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// fn as_fail(&self) -> &Fail;
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// fn as_mut_fail(&mut self) -> &mut Fail;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// impl<T: ResponseError> InternalResponseErrorAsFail for T {
|
|
|
|
// fn as_fail(&self) -> &Fail {
|
|
|
|
// self
|
|
|
|
// }
|
|
|
|
// fn as_mut_fail(&mut self) -> &mut Fail {
|
|
|
|
// self
|
|
|
|
// }
|
|
|
|
// }
|
2017-11-20 04:26:31 +01:00
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
/// Error that can be converted to `Response`
|
2018-12-20 03:34:56 +01:00
|
|
|
pub trait ResponseError: fmt::Debug + fmt::Display {
|
2017-11-16 07:06:28 +01:00
|
|
|
/// Create response for error
|
|
|
|
///
|
|
|
|
/// Internal server error is generated by default.
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
2017-11-16 07:06:28 +01:00
|
|
|
}
|
2018-12-20 03:34:56 +01:00
|
|
|
|
|
|
|
/// Response
|
|
|
|
fn backtrace(&self) -> Option<&Backtrace> {
|
|
|
|
None
|
|
|
|
}
|
2017-11-16 07:06:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.cause, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 05:12:24 +01:00
|
|
|
impl fmt::Debug for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-01-21 06:11:46 +01:00
|
|
|
if let Some(bt) = self.cause.backtrace() {
|
|
|
|
write!(f, "{:?}\n\n{:?}", &self.cause, bt)
|
2018-01-21 05:12:24 +01:00
|
|
|
} else {
|
2018-04-29 18:09:08 +02:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{:?}\n\n{:?}",
|
|
|
|
&self.cause,
|
|
|
|
self.backtrace.as_ref().unwrap()
|
|
|
|
)
|
2018-01-21 05:12:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
/// Convert `Error` to a `Response` instance
|
|
|
|
impl From<Error> for Response {
|
2017-11-16 07:06:28 +01:00
|
|
|
fn from(err: Error) -> Self {
|
2018-10-05 20:04:59 +02:00
|
|
|
Response::from_error(err)
|
2017-11-16 07:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 19:17:15 +01:00
|
|
|
/// `Error` for any error that implements `ResponseError`
|
2018-12-20 03:34:56 +01:00
|
|
|
impl<T: ResponseError + 'static> From<T> for Error {
|
2017-11-16 07:06:28 +01:00
|
|
|
fn from(err: T) -> Error {
|
2018-01-21 05:12:24 +01:00
|
|
|
let backtrace = if err.backtrace().is_none() {
|
|
|
|
Some(Backtrace::new())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-04-14 01:02:01 +02:00
|
|
|
Error {
|
|
|
|
cause: Box::new(err),
|
|
|
|
backtrace,
|
|
|
|
}
|
2017-11-16 07:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// /// Compatibility for `failure::Error`
|
|
|
|
// impl<T> ResponseError for failure::Compat<T> where
|
|
|
|
// T: fmt::Display + fmt::Debug + Sync + Send + 'static
|
|
|
|
// {
|
|
|
|
// }
|
2018-02-06 17:26:50 +01:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// impl From<failure::Error> for Error {
|
|
|
|
// fn from(err: failure::Error) -> Error {
|
|
|
|
// err.compat().into()
|
|
|
|
// }
|
|
|
|
// }
|
2018-02-06 17:26:50 +01:00
|
|
|
|
2017-11-24 19:03:13 +01:00
|
|
|
/// `InternalServerError` for `JsonError`
|
2017-12-02 19:17:15 +01:00
|
|
|
impl ResponseError for JsonError {}
|
2017-11-16 07:06:28 +01:00
|
|
|
|
2018-06-12 18:30:00 +02:00
|
|
|
/// `InternalServerError` for `FormError`
|
|
|
|
impl ResponseError for FormError {}
|
|
|
|
|
2018-05-25 06:03:16 +02:00
|
|
|
/// `InternalServerError` for `TimerError`
|
|
|
|
impl ResponseError for TimerError {}
|
|
|
|
|
2018-03-27 03:18:38 +02:00
|
|
|
/// Return `BAD_REQUEST` for `de::value::Error`
|
|
|
|
impl ResponseError for DeError {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
2018-03-27 03:18:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 01:19:18 +02:00
|
|
|
/// Return `BAD_REQUEST` for `Utf8Error`
|
|
|
|
impl ResponseError for Utf8Error {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
2018-04-03 01:19:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 19:52:43 +01:00
|
|
|
/// Return `InternalServerError` for `HttpError`,
|
|
|
|
/// Response generation can return `HttpError`, so it is internal error
|
2017-12-02 19:17:15 +01:00
|
|
|
impl ResponseError for HttpError {}
|
2017-11-25 19:52:43 +01:00
|
|
|
|
|
|
|
/// Return `InternalServerError` for `io::Error`
|
2017-12-02 23:58:22 +01:00
|
|
|
impl ResponseError for io::Error {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
2017-12-02 23:58:22 +01:00
|
|
|
match self.kind() {
|
2018-10-05 20:04:59 +02:00
|
|
|
io::ErrorKind::NotFound => Response::new(StatusCode::NOT_FOUND),
|
|
|
|
io::ErrorKind::PermissionDenied => Response::new(StatusCode::FORBIDDEN),
|
|
|
|
_ => Response::new(StatusCode::INTERNAL_SERVER_ERROR),
|
2017-12-02 23:58:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 19:52:43 +01:00
|
|
|
|
2018-03-06 01:45:54 +01:00
|
|
|
/// `BadRequest` for `InvalidHeaderValue`
|
|
|
|
impl ResponseError for header::InvalidHeaderValue {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2018-03-06 01:45:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `BadRequest` for `InvalidHeaderValue`
|
|
|
|
impl ResponseError for header::InvalidHeaderValueBytes {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
2018-03-06 01:45:54 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-25 19:52:43 +01:00
|
|
|
|
2017-12-21 05:30:54 +01:00
|
|
|
/// `InternalServerError` for `futures::Canceled`
|
|
|
|
impl ResponseError for Canceled {}
|
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
// /// `InternalServerError` for `actix::MailboxError`
|
|
|
|
// impl ResponseError for MailboxError {}
|
2018-02-07 22:57:58 +01:00
|
|
|
|
2017-11-20 04:26:31 +01:00
|
|
|
/// A set of errors that can occur during parsing HTTP streams
|
2018-12-20 03:34:56 +01:00
|
|
|
#[derive(Debug, Display)]
|
2017-10-13 23:43:17 +02:00
|
|
|
pub enum ParseError {
|
2017-11-20 04:26:31 +01:00
|
|
|
/// An invalid `Method`, such as `GE.T`.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Invalid Method specified")]
|
2017-10-07 06:48:14 +02:00
|
|
|
Method,
|
|
|
|
/// An invalid `Uri`, such as `exam ple.domain`.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Uri error: {}", _0)]
|
2018-03-23 02:08:12 +01:00
|
|
|
Uri(InvalidUri),
|
2017-10-07 06:48:14 +02:00
|
|
|
/// An invalid `HttpVersion`, such as `HTP/1.1`
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Invalid HTTP version specified")]
|
2017-10-07 06:48:14 +02:00
|
|
|
Version,
|
|
|
|
/// An invalid `Header`.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Invalid Header provided")]
|
2017-10-07 06:48:14 +02:00
|
|
|
Header,
|
|
|
|
/// A message head is too large to be reasonable.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Message head is too large")]
|
2017-10-07 06:48:14 +02:00
|
|
|
TooLarge,
|
|
|
|
/// A message reached EOF, but is not complete.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Message is incomplete")]
|
2017-10-07 06:48:14 +02:00
|
|
|
Incomplete,
|
|
|
|
/// An invalid `Status`, such as `1337 ELITE`.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Invalid Status provided")]
|
2017-10-07 06:48:14 +02:00
|
|
|
Status,
|
|
|
|
/// A timeout occurred waiting for an IO event.
|
|
|
|
#[allow(dead_code)]
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Timeout")]
|
2017-10-07 06:48:14 +02:00
|
|
|
Timeout,
|
2018-04-14 01:02:01 +02:00
|
|
|
/// An `io::Error` that occurred while trying to read or write to a network
|
|
|
|
/// stream.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "IO error: {}", _0)]
|
|
|
|
Io(IoError),
|
2017-10-07 06:48:14 +02:00
|
|
|
/// Parsing a field as string failed
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "UTF8 error: {}", _0)]
|
|
|
|
Utf8(Utf8Error),
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 07:06:28 +01:00
|
|
|
/// Return `BadRequest` for `ParseError`
|
2017-12-02 19:17:15 +01:00
|
|
|
impl ResponseError for ParseError {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<IoError> for ParseError {
|
|
|
|
fn from(err: IoError) -> ParseError {
|
|
|
|
ParseError::Io(err)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 02:08:12 +01:00
|
|
|
impl From<InvalidUri> for ParseError {
|
|
|
|
fn from(err: InvalidUri) -> ParseError {
|
|
|
|
ParseError::Uri(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<Utf8Error> for ParseError {
|
|
|
|
fn from(err: Utf8Error) -> ParseError {
|
|
|
|
ParseError::Utf8(err)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<FromUtf8Error> for ParseError {
|
|
|
|
fn from(err: FromUtf8Error) -> ParseError {
|
|
|
|
ParseError::Utf8(err.utf8_error())
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<httparse::Error> for ParseError {
|
|
|
|
fn from(err: httparse::Error) -> ParseError {
|
2017-10-07 06:48:14 +02:00
|
|
|
match err {
|
2018-04-14 01:02:01 +02:00
|
|
|
httparse::Error::HeaderName
|
|
|
|
| httparse::Error::HeaderValue
|
|
|
|
| httparse::Error::NewLine
|
|
|
|
| httparse::Error::Token => ParseError::Header,
|
2017-10-13 23:43:17 +02:00
|
|
|
httparse::Error::Status => ParseError::Status,
|
|
|
|
httparse::Error::TooManyHeaders => ParseError::TooLarge,
|
|
|
|
httparse::Error::Version => ParseError::Version,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 05:41:09 +01:00
|
|
|
#[derive(Display, Debug, From)]
|
2017-11-20 04:26:31 +01:00
|
|
|
/// A set of errors that can occur during payload parsing
|
2017-11-16 07:06:28 +01:00
|
|
|
pub enum PayloadError {
|
|
|
|
/// A payload reached EOF, but is not complete.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "A payload reached EOF, but is not complete.")]
|
2018-11-14 18:38:16 +01:00
|
|
|
Incomplete(Option<io::Error>),
|
2017-11-16 07:06:28 +01:00
|
|
|
/// Content encoding stream corruption
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Can not decode content-encoding.")]
|
2017-11-16 07:06:28 +01:00
|
|
|
EncodingCorrupted,
|
2018-01-03 18:23:58 +01:00
|
|
|
/// A payload reached size limit.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "A payload reached size limit.")]
|
2018-01-03 18:23:58 +01:00
|
|
|
Overflow,
|
|
|
|
/// A payload length is unknown.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "A payload length is unknown.")]
|
2018-01-03 18:23:58 +01:00
|
|
|
UnknownLength,
|
2019-01-29 05:41:09 +01:00
|
|
|
/// Http2 payload error
|
|
|
|
#[display(fmt = "{}", _0)]
|
|
|
|
H2Payload(h2::Error),
|
2017-10-13 23:43:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
impl From<io::Error> for PayloadError {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
PayloadError::Incomplete(Some(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 21:27:43 +02:00
|
|
|
/// `PayloadError` returns two possible results:
|
|
|
|
///
|
|
|
|
/// - `Overflow` returns `PayloadTooLarge`
|
|
|
|
/// - Other errors returns `BadRequest`
|
2018-06-02 21:20:22 +02:00
|
|
|
impl ResponseError for PayloadError {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
2018-06-02 21:20:22 +02:00
|
|
|
match *self {
|
2018-10-05 20:04:59 +02:00
|
|
|
PayloadError::Overflow => Response::new(StatusCode::PAYLOAD_TOO_LARGE),
|
|
|
|
_ => Response::new(StatusCode::BAD_REQUEST),
|
2018-06-02 21:20:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 07:36:28 +01:00
|
|
|
|
2017-11-16 07:06:28 +01:00
|
|
|
/// Return `BadRequest` for `cookie::ParseError`
|
2017-12-02 19:17:15 +01:00
|
|
|
impl ResponseError for cookie::ParseError {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
2017-10-13 23:43:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
#[derive(Debug, Display)]
|
2018-10-05 05:02:10 +02:00
|
|
|
/// A set of errors that can occur during dispatching http requests
|
2018-10-07 18:48:53 +02:00
|
|
|
pub enum DispatchError<E: fmt::Debug> {
|
2018-10-05 05:02:10 +02:00
|
|
|
/// Service error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Service specific error: {:?}", _0)]
|
2018-10-05 05:02:10 +02:00
|
|
|
Service(E),
|
2017-11-16 07:06:28 +01:00
|
|
|
|
2018-10-05 05:02:10 +02:00
|
|
|
/// An `io::Error` that occurred while trying to read or write to a network
|
|
|
|
/// stream.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "IO error: {}", _0)]
|
2018-10-05 05:02:10 +02:00
|
|
|
Io(io::Error),
|
2017-11-16 07:06:28 +01:00
|
|
|
|
2018-10-05 05:02:10 +02:00
|
|
|
/// Http request parse error.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Parse error: {}", _0)]
|
2018-10-05 05:02:10 +02:00
|
|
|
Parse(ParseError),
|
2017-10-13 23:43:17 +02:00
|
|
|
|
2018-10-05 05:02:10 +02:00
|
|
|
/// The first request did not complete within the specified timeout.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "The first request did not complete within the specified timeout")]
|
2018-10-05 05:02:10 +02:00
|
|
|
SlowRequestTimeout,
|
|
|
|
|
2018-10-05 08:39:11 +02:00
|
|
|
/// Disconnect timeout. Makes sense for ssl streams.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Connection shutdown timeout")]
|
2018-10-05 08:39:11 +02:00
|
|
|
DisconnectTimeout,
|
2018-10-05 05:02:10 +02:00
|
|
|
|
|
|
|
/// Payload is not consumed
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Task is completed but request's payload is not consumed")]
|
2018-10-05 05:02:10 +02:00
|
|
|
PayloadIsNotConsumed,
|
|
|
|
|
|
|
|
/// Malformed request
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Malformed request")]
|
2018-10-05 05:02:10 +02:00
|
|
|
MalformedRequest,
|
|
|
|
|
|
|
|
/// Internal error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Internal error")]
|
2018-10-05 05:02:10 +02:00
|
|
|
InternalError,
|
|
|
|
|
|
|
|
/// Unknown error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Unknown error")]
|
2018-10-05 05:02:10 +02:00
|
|
|
Unknown,
|
2017-10-20 01:22:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 18:48:53 +02:00
|
|
|
impl<E: fmt::Debug> From<ParseError> for DispatchError<E> {
|
2018-10-05 05:02:10 +02:00
|
|
|
fn from(err: ParseError) -> Self {
|
|
|
|
DispatchError::Parse(err)
|
|
|
|
}
|
2017-11-20 04:51:14 +01:00
|
|
|
}
|
|
|
|
|
2018-10-07 18:48:53 +02:00
|
|
|
impl<E: fmt::Debug> From<io::Error> for DispatchError<E> {
|
2018-10-05 05:02:10 +02:00
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
DispatchError::Io(err)
|
2017-11-20 04:51:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 19:53:45 +02:00
|
|
|
/// A set of error that can occure during parsing content type
|
2018-12-20 03:34:56 +01:00
|
|
|
#[derive(PartialEq, Debug, Display)]
|
2018-02-27 20:31:54 +01:00
|
|
|
pub enum ContentTypeError {
|
|
|
|
/// Can not parse content type
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Can not parse content type")]
|
2018-02-27 20:31:54 +01:00
|
|
|
ParseError,
|
|
|
|
/// Unknown content encoding
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Unknown content encoding")]
|
2018-02-27 20:31:54 +01:00
|
|
|
UnknownEncoding,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return `BadRequest` for `ContentTypeError`
|
|
|
|
impl ResponseError for ContentTypeError {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
2018-02-27 20:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 07:00:25 +01:00
|
|
|
/// A set of errors that can occur during parsing urlencoded payloads
|
2018-12-20 03:34:56 +01:00
|
|
|
#[derive(Debug, Display, From)]
|
2017-11-27 07:00:25 +01:00
|
|
|
pub enum UrlencodedError {
|
|
|
|
/// Can not decode chunked transfer encoding
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Can not decode chunked transfer encoding")]
|
2017-11-27 07:00:25 +01:00
|
|
|
Chunked,
|
2018-06-03 00:10:48 +02:00
|
|
|
/// Payload size is bigger than allowed. (default: 256kB)
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Urlencoded payload size is bigger than allowed. (default: 256kB)")]
|
2017-11-27 07:00:25 +01:00
|
|
|
Overflow,
|
|
|
|
/// Payload size is now known
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Payload size is now known")]
|
2017-11-27 07:00:25 +01:00
|
|
|
UnknownLength,
|
|
|
|
/// Content type error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Content type error")]
|
2017-11-27 07:00:25 +01:00
|
|
|
ContentType,
|
2018-02-27 20:31:54 +01:00
|
|
|
/// Parse error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Parse error")]
|
2018-02-27 20:31:54 +01:00
|
|
|
Parse,
|
2017-12-19 23:03:01 +01:00
|
|
|
/// Payload error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
|
|
|
Payload(PayloadError),
|
2017-11-27 07:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return `BadRequest` for `UrlencodedError`
|
2017-12-02 19:17:15 +01:00
|
|
|
impl ResponseError for UrlencodedError {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
2018-01-31 21:34:58 +01:00
|
|
|
match *self {
|
2018-10-05 20:04:59 +02:00
|
|
|
UrlencodedError::Overflow => Response::new(StatusCode::PAYLOAD_TOO_LARGE),
|
|
|
|
UrlencodedError::UnknownLength => Response::new(StatusCode::LENGTH_REQUIRED),
|
|
|
|
_ => Response::new(StatusCode::BAD_REQUEST),
|
2018-01-31 21:34:58 +01:00
|
|
|
}
|
2017-12-02 19:17:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 05:30:54 +01:00
|
|
|
/// A set of errors that can occur during parsing json payloads
|
2018-12-20 03:34:56 +01:00
|
|
|
#[derive(Debug, Display, From)]
|
2017-12-21 05:30:54 +01:00
|
|
|
pub enum JsonPayloadError {
|
2018-06-03 00:10:48 +02:00
|
|
|
/// Payload size is bigger than allowed. (default: 256kB)
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Json payload size is bigger than allowed. (default: 256kB)")]
|
2017-12-21 05:30:54 +01:00
|
|
|
Overflow,
|
|
|
|
/// Content type error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Content type error")]
|
2017-12-21 05:30:54 +01:00
|
|
|
ContentType,
|
|
|
|
/// Deserialize error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Json deserialize error: {}", _0)]
|
|
|
|
Deserialize(JsonError),
|
2017-12-21 05:30:54 +01:00
|
|
|
/// Payload error
|
2018-12-20 03:34:56 +01:00
|
|
|
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
|
|
|
Payload(PayloadError),
|
2017-12-21 05:30:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return `BadRequest` for `UrlencodedError`
|
|
|
|
impl ResponseError for JsonPayloadError {
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
2018-01-31 21:34:58 +01:00
|
|
|
match *self {
|
2018-10-05 20:04:59 +02:00
|
|
|
JsonPayloadError::Overflow => Response::new(StatusCode::PAYLOAD_TOO_LARGE),
|
|
|
|
_ => Response::new(StatusCode::BAD_REQUEST),
|
2018-01-31 21:34:58 +01:00
|
|
|
}
|
2017-12-21 05:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 21:59:36 +02:00
|
|
|
/// Error type returned when reading body as lines.
|
2018-12-20 03:34:56 +01:00
|
|
|
#[derive(From)]
|
2018-06-13 21:59:36 +02:00
|
|
|
pub enum ReadlinesError {
|
2018-06-13 23:19:48 +02:00
|
|
|
/// Error when decoding a line.
|
2018-06-13 21:59:36 +02:00
|
|
|
EncodingError,
|
2018-06-13 23:19:48 +02:00
|
|
|
/// Payload error.
|
2018-06-13 21:59:36 +02:00
|
|
|
PayloadError(PayloadError),
|
2018-06-13 23:19:48 +02:00
|
|
|
/// Line limit exceeded.
|
2018-06-13 21:59:36 +02:00
|
|
|
LimitOverflow,
|
2018-06-13 23:19:48 +02:00
|
|
|
/// ContentType error.
|
2018-06-13 21:59:36 +02:00
|
|
|
ContentTypeError(ContentTypeError),
|
|
|
|
}
|
|
|
|
|
2018-01-21 06:11:46 +01:00
|
|
|
/// Helper type that can wrap any error and generate custom response.
|
2017-12-09 00:25:37 +01:00
|
|
|
///
|
2018-04-14 01:02:01 +02:00
|
|
|
/// In following example any `io::Error` will be converted into "BAD REQUEST"
|
2018-05-06 18:07:30 +02:00
|
|
|
/// response as opposite to *INTERNAL SERVER ERROR* which is defined by
|
2018-04-14 01:02:01 +02:00
|
|
|
/// default.
|
2017-12-09 00:25:37 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
2018-10-05 06:14:18 +02:00
|
|
|
/// # extern crate actix_http;
|
|
|
|
/// # use std::io;
|
|
|
|
/// # use actix_http::*;
|
2017-12-09 00:25:37 +01:00
|
|
|
///
|
2018-10-05 06:14:18 +02:00
|
|
|
/// fn index(req: Request) -> Result<&'static str> {
|
|
|
|
/// Err(error::ErrorBadRequest(io::Error::new(io::ErrorKind::Other, "error")))
|
2017-12-09 00:25:37 +01:00
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2018-01-21 06:11:46 +01:00
|
|
|
pub struct InternalError<T> {
|
|
|
|
cause: T,
|
2018-04-14 04:10:42 +02:00
|
|
|
status: InternalErrorType,
|
2018-01-21 06:11:46 +01:00
|
|
|
backtrace: Backtrace,
|
|
|
|
}
|
|
|
|
|
2018-04-14 04:10:42 +02:00
|
|
|
enum InternalErrorType {
|
|
|
|
Status(StatusCode),
|
2018-12-20 03:34:56 +01:00
|
|
|
Response(RefCell<Option<Response>>),
|
2018-04-14 04:10:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 06:11:46 +01:00
|
|
|
impl<T> InternalError<T> {
|
2018-04-14 04:14:14 +02:00
|
|
|
/// Create `InternalError` instance
|
2018-02-26 23:33:56 +01:00
|
|
|
pub fn new(cause: T, status: StatusCode) -> Self {
|
2018-01-21 06:11:46 +01:00
|
|
|
InternalError {
|
2018-02-26 23:33:56 +01:00
|
|
|
cause,
|
2018-04-14 04:10:42 +02:00
|
|
|
status: InternalErrorType::Status(status),
|
|
|
|
backtrace: Backtrace::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
/// Create `InternalError` with predefined `Response`.
|
|
|
|
pub fn from_response(cause: T, response: Response) -> Self {
|
2018-04-14 04:10:42 +02:00
|
|
|
InternalError {
|
|
|
|
cause,
|
2018-12-20 03:34:56 +01:00
|
|
|
status: InternalErrorType::Response(RefCell::new(Some(response))),
|
2018-01-21 06:11:46 +01:00
|
|
|
backtrace: Backtrace::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for InternalError<T>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + 'static,
|
2018-01-21 06:11:46 +01:00
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(&self.cause, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> fmt::Display for InternalError<T>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Display + 'static,
|
2018-01-21 06:11:46 +01:00
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-04-19 16:55:09 +02:00
|
|
|
fmt::Display::fmt(&self.cause, f)
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ResponseError for InternalError<T>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-01-21 06:11:46 +01:00
|
|
|
{
|
2018-12-20 03:34:56 +01:00
|
|
|
fn backtrace(&self) -> Option<&Backtrace> {
|
|
|
|
Some(&self.backtrace)
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
fn error_response(&self) -> Response {
|
2018-04-14 04:10:42 +02:00
|
|
|
match self.status {
|
2018-10-05 20:04:59 +02:00
|
|
|
InternalErrorType::Status(st) => Response::new(st),
|
2018-04-14 04:10:42 +02:00
|
|
|
InternalErrorType::Response(ref resp) => {
|
2018-12-20 03:34:56 +01:00
|
|
|
if let Some(resp) = resp.borrow_mut().take() {
|
|
|
|
resp
|
2018-04-14 04:10:42 +02:00
|
|
|
} else {
|
2018-10-05 20:04:59 +02:00
|
|
|
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
2018-04-14 04:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 18:59:20 +02:00
|
|
|
/// Convert Response to a Error
|
|
|
|
impl From<Response> for Error {
|
|
|
|
fn from(res: Response) -> Error {
|
|
|
|
InternalError::from_response("", res).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate *BAD
|
|
|
|
/// REQUEST* response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorBadRequest<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::BAD_REQUEST).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:25:37 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate
|
|
|
|
/// *UNAUTHORIZED* response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorUnauthorized<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::UNAUTHORIZED).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:25:37 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate *FORBIDDEN*
|
|
|
|
/// response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorForbidden<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::FORBIDDEN).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:25:37 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate *NOT FOUND*
|
|
|
|
/// response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorNotFound<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::NOT_FOUND).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:25:37 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate *METHOD NOT
|
|
|
|
/// ALLOWED* response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorMethodNotAllowed<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::METHOD_NOT_ALLOWED).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:52:46 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate *REQUEST
|
|
|
|
/// TIMEOUT* response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorRequestTimeout<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::REQUEST_TIMEOUT).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:52:46 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate *CONFLICT*
|
|
|
|
/// response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorConflict<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::CONFLICT).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:52:46 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate *GONE*
|
|
|
|
/// response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorGone<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::GONE).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:52:46 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate
|
|
|
|
/// *PRECONDITION FAILED* response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorPreconditionFailed<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::PRECONDITION_FAILED).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:52:46 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Helper function that creates wrapper of any error and generate
|
|
|
|
/// *EXPECTATION FAILED* response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorExpectationFailed<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::EXPECTATION_FAILED).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:52:46 +01:00
|
|
|
|
2018-03-18 22:18:47 +01:00
|
|
|
/// Helper function that creates wrapper of any error and
|
|
|
|
/// generate *INTERNAL SERVER ERROR* response.
|
2018-01-21 06:11:46 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-03-18 22:18:47 +01:00
|
|
|
pub fn ErrorInternalServerError<T>(err: T) -> Error
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-03-18 22:18:47 +01:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR).into()
|
2018-01-21 06:11:46 +01:00
|
|
|
}
|
2017-12-09 00:25:37 +01:00
|
|
|
|
2018-05-10 17:26:38 +02:00
|
|
|
/// Helper function that creates wrapper of any error and
|
|
|
|
/// generate *NOT IMPLEMENTED* response.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn ErrorNotImplemented<T>(err: T) -> Error
|
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-05-10 17:26:38 +02:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::NOT_IMPLEMENTED).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function that creates wrapper of any error and
|
|
|
|
/// generate *BAD GATEWAY* response.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn ErrorBadGateway<T>(err: T) -> Error
|
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-05-10 17:26:38 +02:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::BAD_GATEWAY).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function that creates wrapper of any error and
|
|
|
|
/// generate *SERVICE UNAVAILABLE* response.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn ErrorServiceUnavailable<T>(err: T) -> Error
|
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-05-10 17:26:38 +02:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::SERVICE_UNAVAILABLE).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function that creates wrapper of any error and
|
|
|
|
/// generate *GATEWAY TIMEOUT* response.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn ErrorGatewayTimeout<T>(err: T) -> Error
|
|
|
|
where
|
2018-12-20 03:34:56 +01:00
|
|
|
T: fmt::Debug + fmt::Display + 'static,
|
2018-05-10 17:26:38 +02:00
|
|
|
{
|
|
|
|
InternalError::new(err, StatusCode::GATEWAY_TIMEOUT).into()
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-04-14 01:02:01 +02:00
|
|
|
use super::*;
|
|
|
|
use cookie::ParseError as CookieParseError;
|
|
|
|
use http::{Error as HttpError, StatusCode};
|
|
|
|
use httparse;
|
2017-10-07 06:48:14 +02:00
|
|
|
use std::error::Error as StdError;
|
|
|
|
use std::io;
|
2017-10-15 08:14:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_response() {
|
2018-10-05 20:04:59 +02:00
|
|
|
let resp: Response = ParseError::Incomplete.error_response();
|
2017-10-15 08:14:26 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let resp: Response = CookieParseError::EmptyName.error_response();
|
2017-10-15 08:14:26 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2017-10-22 18:45:53 +02:00
|
|
|
|
|
|
|
let err: HttpError = StatusCode::from_u16(10000).err().unwrap().into();
|
2018-10-05 20:04:59 +02:00
|
|
|
let resp: Response = err.error_response();
|
2017-10-22 18:45:53 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
#[test]
|
2018-12-20 03:34:56 +01:00
|
|
|
fn test_as_response() {
|
2017-10-07 06:48:14 +02:00
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
2018-12-20 03:34:56 +01:00
|
|
|
let e: Error = ParseError::Io(orig).into();
|
|
|
|
assert_eq!(format!("{}", e.as_response_error()), "IO error: other");
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 23:21:50 +02:00
|
|
|
#[test]
|
|
|
|
fn test_backtrace() {
|
2018-05-02 15:07:30 +02:00
|
|
|
let e = ErrorBadRequest("err");
|
2018-05-04 01:26:42 +02:00
|
|
|
let _ = e.backtrace();
|
2018-04-29 23:21:50 +02:00
|
|
|
}
|
|
|
|
|
2017-11-20 04:26:31 +01:00
|
|
|
#[test]
|
|
|
|
fn test_error_cause() {
|
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
|
|
|
let desc = orig.description().to_owned();
|
|
|
|
let e = Error::from(orig);
|
2018-12-20 03:34:56 +01:00
|
|
|
assert_eq!(format!("{}", e.as_response_error()), desc);
|
2017-11-20 04:26:31 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 04:58:47 +01:00
|
|
|
#[test]
|
|
|
|
fn test_error_display() {
|
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
|
|
|
let desc = orig.description().to_owned();
|
|
|
|
let e = Error::from(orig);
|
|
|
|
assert_eq!(format!("{}", e), desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_error_http_response() {
|
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
|
|
|
let e = Error::from(orig);
|
2018-10-05 20:04:59 +02:00
|
|
|
let resp: Response = e.into();
|
2017-11-20 04:58:47 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
macro_rules! from {
|
2018-05-07 22:50:43 +02:00
|
|
|
($from:expr => $error:pat) => {
|
2017-10-14 01:33:23 +02:00
|
|
|
match ParseError::from($from) {
|
2017-10-07 06:48:14 +02:00
|
|
|
e @ $error => {
|
2017-11-16 07:06:28 +01:00
|
|
|
assert!(format!("{}", e).len() >= 5);
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
|
|
|
e => unreachable!("{:?}", e),
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
};
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! from_and_cause {
|
2018-05-07 22:50:43 +02:00
|
|
|
($from:expr => $error:pat) => {
|
2017-10-14 01:33:23 +02:00
|
|
|
match ParseError::from($from) {
|
2017-10-07 06:48:14 +02:00
|
|
|
e @ $error => {
|
2018-12-20 03:34:56 +01:00
|
|
|
let desc = format!("{}", e);
|
2019-01-29 19:14:00 +01:00
|
|
|
assert_eq!(desc, format!("IO error: {}", $from.description()));
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
|
|
|
_ => unreachable!("{:?}", $from),
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
};
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
2019-01-29 19:14:00 +01:00
|
|
|
from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => ParseError::Io(..));
|
2017-10-14 01:33:23 +02:00
|
|
|
from!(httparse::Error::HeaderName => ParseError::Header);
|
|
|
|
from!(httparse::Error::HeaderName => ParseError::Header);
|
|
|
|
from!(httparse::Error::HeaderValue => ParseError::Header);
|
|
|
|
from!(httparse::Error::NewLine => ParseError::Header);
|
|
|
|
from!(httparse::Error::Status => ParseError::Status);
|
|
|
|
from!(httparse::Error::Token => ParseError::Header);
|
|
|
|
from!(httparse::Error::TooManyHeaders => ParseError::TooLarge);
|
|
|
|
from!(httparse::Error::Version => ParseError::Version);
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2018-02-06 17:26:50 +01:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// #[test]
|
|
|
|
// fn failure_error() {
|
|
|
|
// const NAME: &str = "RUST_BACKTRACE";
|
|
|
|
// let old_tb = env::var(NAME);
|
|
|
|
// env::set_var(NAME, "0");
|
|
|
|
// let error = failure::err_msg("Hello!");
|
|
|
|
// let resp: Error = error.into();
|
|
|
|
// assert_eq!(
|
|
|
|
// format!("{:?}", resp),
|
|
|
|
// "Compat { error: ErrorMessage { msg: \"Hello!\" } }\n\n"
|
|
|
|
// );
|
|
|
|
// match old_tb {
|
|
|
|
// Ok(x) => env::set_var(NAME, x),
|
|
|
|
// _ => env::remove_var(NAME),
|
|
|
|
// }
|
|
|
|
// }
|
2018-04-14 04:10:42 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_internal_error() {
|
2018-10-05 06:14:18 +02:00
|
|
|
let err =
|
2018-10-05 20:04:59 +02:00
|
|
|
InternalError::from_response(ParseError::Method, Response::Ok().into());
|
|
|
|
let resp: Response = err.error_response();
|
2018-04-14 04:10:42 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
2018-05-10 18:13:26 +02:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// #[test]
|
|
|
|
// fn test_error_downcasting_direct() {
|
|
|
|
// #[derive(Debug, Display)]
|
|
|
|
// #[display(fmt = "demo error")]
|
|
|
|
// struct DemoError;
|
2018-06-07 18:53:27 +02:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// impl ResponseError for DemoError {}
|
2018-06-07 18:53:27 +02:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// let err: Error = DemoError.into();
|
|
|
|
// let err_ref: &DemoError = err.downcast_ref().unwrap();
|
|
|
|
// assert_eq!(err_ref.to_string(), "demo error");
|
|
|
|
// }
|
2018-06-07 18:53:27 +02:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// #[test]
|
|
|
|
// fn test_error_downcasting_compat() {
|
|
|
|
// #[derive(Debug, Display)]
|
|
|
|
// #[display(fmt = "demo error")]
|
|
|
|
// struct DemoError;
|
2018-06-07 18:53:27 +02:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// impl ResponseError for DemoError {}
|
2018-06-07 18:53:27 +02:00
|
|
|
|
2018-12-20 03:34:56 +01:00
|
|
|
// let err: Error = failure::Error::from(DemoError).into();
|
|
|
|
// let err_ref: &DemoError = err.downcast_ref().unwrap();
|
|
|
|
// assert_eq!(err_ref.to_string(), "demo error");
|
|
|
|
// }
|
2018-06-07 18:53:27 +02:00
|
|
|
|
2018-05-10 18:13:26 +02:00
|
|
|
#[test]
|
|
|
|
fn test_error_helpers() {
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorBadRequest("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorUnauthorized("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::UNAUTHORIZED);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorForbidden("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::FORBIDDEN);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorNotFound("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorMethodNotAllowed("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorRequestTimeout("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::REQUEST_TIMEOUT);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorConflict("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::CONFLICT);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorGone("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::GONE);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorPreconditionFailed("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::PRECONDITION_FAILED);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorExpectationFailed("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::EXPECTATION_FAILED);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorInternalServerError("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorNotImplemented("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::NOT_IMPLEMENTED);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorBadGateway("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::BAD_GATEWAY);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorServiceUnavailable("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
let r: Response = ErrorGatewayTimeout("err").into();
|
2018-05-10 18:13:26 +02:00
|
|
|
assert_eq!(r.status(), StatusCode::GATEWAY_TIMEOUT);
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|