2019-03-26 19:54:35 +01:00
|
|
|
//! Error and Result module
|
2020-08-24 11:13:35 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Error};
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2021-04-12 11:30:28 +02:00
|
|
|
use derive_more::{Display, Error, From};
|
2023-07-17 03:38:12 +02:00
|
|
|
pub use http::Error as HttpError;
|
2021-06-17 18:57:58 +02:00
|
|
|
use http::{uri::InvalidUri, StatusCode};
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2022-01-31 22:22:23 +01:00
|
|
|
use crate::{body::BoxBody, Response};
|
2021-02-13 16:08:43 +01:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
pub struct Error {
|
2021-06-17 18:57:58 +02:00
|
|
|
inner: Box<ErrorInner>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ErrorInner {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
kind: Kind,
|
|
|
|
cause: Option<Box<dyn StdError>>,
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
2021-06-17 18:57:58 +02:00
|
|
|
fn new(kind: Kind) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: Box::new(ErrorInner { kind, cause: None }),
|
|
|
|
}
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
2019-07-18 13:25:50 +02:00
|
|
|
|
2021-11-16 23:10:30 +01:00
|
|
|
pub(crate) fn with_cause(mut self, cause: impl Into<Box<dyn StdError>>) -> Self {
|
|
|
|
self.inner.cause = Some(cause.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_http() -> Self {
|
|
|
|
Self::new(Kind::Http)
|
2019-07-18 13:25:50 +02:00
|
|
|
}
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_parse() -> Self {
|
|
|
|
Self::new(Kind::Parse)
|
2019-04-10 21:43:31 +02:00
|
|
|
}
|
2019-07-18 13:25:50 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_payload() -> Self {
|
|
|
|
Self::new(Kind::Payload)
|
|
|
|
}
|
2019-07-18 13:25:50 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_body() -> Self {
|
|
|
|
Self::new(Kind::Body)
|
|
|
|
}
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_send_response() -> Self {
|
|
|
|
Self::new(Kind::SendResponse)
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 09:45:28 +01:00
|
|
|
#[allow(unused)] // available for future use
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_io() -> Self {
|
|
|
|
Self::new(Kind::Io)
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 23:10:30 +01:00
|
|
|
#[allow(unused)] // used in encoder behind feature flag so ignore unused warning
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_encoder() -> Self {
|
|
|
|
Self::new(Kind::Encoder)
|
2019-04-04 22:23:38 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 22:22:23 +01:00
|
|
|
#[allow(unused)] // used with `ws` feature flag
|
2021-06-17 18:57:58 +02:00
|
|
|
pub(crate) fn new_ws() -> Self {
|
|
|
|
Self::new(Kind::Ws)
|
2019-12-05 18:35:43 +01:00
|
|
|
}
|
2019-09-17 02:57:39 +02:00
|
|
|
}
|
|
|
|
|
2021-12-04 20:40:47 +01:00
|
|
|
impl From<Error> for Response<BoxBody> {
|
2019-03-26 19:54:35 +01:00
|
|
|
fn from(err: Error) -> Self {
|
2021-12-04 20:40:47 +01:00
|
|
|
// TODO: more appropriate error status codes, usage assessment needed
|
2021-06-17 18:57:58 +02:00
|
|
|
let status_code = match err.inner.kind {
|
|
|
|
Kind::Parse => StatusCode::BAD_REQUEST,
|
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
};
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2021-12-04 20:40:47 +01:00
|
|
|
Response::new(status_code).set_body(BoxBody::new(err.to_string()))
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
|
2021-11-16 23:10:30 +01:00
|
|
|
pub(crate) enum Kind {
|
2021-06-17 18:57:58 +02:00
|
|
|
#[display(fmt = "error processing HTTP")]
|
|
|
|
Http,
|
2019-04-06 01:46:44 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
#[display(fmt = "error parsing HTTP message")]
|
|
|
|
Parse,
|
2021-05-05 19:36:02 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
#[display(fmt = "request payload read error")]
|
|
|
|
Payload,
|
2019-04-06 01:46:44 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
#[display(fmt = "response body write error")]
|
|
|
|
Body,
|
2019-04-12 00:12:23 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
#[display(fmt = "send response error")]
|
|
|
|
SendResponse,
|
|
|
|
|
|
|
|
#[display(fmt = "error in WebSocket process")]
|
|
|
|
Ws,
|
|
|
|
|
|
|
|
#[display(fmt = "connection error")]
|
|
|
|
Io,
|
|
|
|
|
|
|
|
#[display(fmt = "encoder error")]
|
|
|
|
Encoder,
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
impl fmt::Debug for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-02-22 08:04:23 +01:00
|
|
|
f.debug_struct("actix_http::Error")
|
|
|
|
.field("kind", &self.inner.kind)
|
|
|
|
.field("cause", &self.inner.cause)
|
|
|
|
.finish()
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self.inner.cause.as_ref() {
|
|
|
|
Some(err) => write!(f, "{}: {}", &self.inner.kind, err),
|
|
|
|
None => write!(f, "{}", &self.inner.kind),
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
impl StdError for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
2021-06-26 16:33:43 +02:00
|
|
|
self.inner.cause.as_ref().map(Box::as_ref)
|
2021-06-17 18:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::convert::Infallible> for Error {
|
|
|
|
fn from(err: std::convert::Infallible) -> Self {
|
|
|
|
match err {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HttpError> for Error {
|
|
|
|
fn from(err: HttpError) -> Self {
|
|
|
|
Self::new_http().with_cause(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 22:22:23 +01:00
|
|
|
#[cfg(feature = "ws")]
|
|
|
|
impl From<crate::ws::HandshakeError> for Error {
|
|
|
|
fn from(err: crate::ws::HandshakeError) -> Self {
|
2021-06-17 18:57:58 +02:00
|
|
|
Self::new_ws().with_cause(err)
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 22:22:23 +01:00
|
|
|
#[cfg(feature = "ws")]
|
|
|
|
impl From<crate::ws::ProtocolError> for Error {
|
|
|
|
fn from(err: crate::ws::ProtocolError) -> Self {
|
2021-12-04 20:40:47 +01:00
|
|
|
Self::new_ws().with_cause(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 07:07:59 +02:00
|
|
|
/// A set of errors that can occur during parsing HTTP streams.
|
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[non_exhaustive]
|
2019-03-26 19:54:35 +01:00
|
|
|
pub enum ParseError {
|
|
|
|
/// An invalid `Method`, such as `GE.T`.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "invalid method specified")]
|
2019-03-26 19:54:35 +01:00
|
|
|
Method,
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// An invalid `Uri`, such as `exam ple.domain`.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "URI error: {}", _0)]
|
2019-03-26 19:54:35 +01:00
|
|
|
Uri(InvalidUri),
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// An invalid `HttpVersion`, such as `HTP/1.1`
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "invalid HTTP version specified")]
|
2019-03-26 19:54:35 +01:00
|
|
|
Version,
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// An invalid `Header`.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "invalid Header provided")]
|
2019-03-26 19:54:35 +01:00
|
|
|
Header,
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// A message head is too large to be reasonable.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "message head is too large")]
|
2019-03-26 19:54:35 +01:00
|
|
|
TooLarge,
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// A message reached EOF, but is not complete.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "message is incomplete")]
|
2019-03-26 19:54:35 +01:00
|
|
|
Incomplete,
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// An invalid `Status`, such as `1337 ELITE`.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "invalid status provided")]
|
2019-03-26 19:54:35 +01:00
|
|
|
Status,
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// A timeout occurred waiting for an IO event.
|
|
|
|
#[allow(dead_code)]
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "timeout")]
|
2019-03-26 19:54:35 +01:00
|
|
|
Timeout,
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2023-03-13 18:17:02 +01:00
|
|
|
/// An I/O error that occurred while trying to read or write to a network stream.
|
|
|
|
#[display(fmt = "I/O error: {}", _0)]
|
2019-03-28 19:08:24 +01:00
|
|
|
Io(io::Error),
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2021-08-12 21:18:09 +02:00
|
|
|
/// Parsing a field as string failed.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "UTF-8 error: {}", _0)]
|
2019-03-26 19:54:35 +01:00
|
|
|
Utf8(Utf8Error),
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:08:24 +01:00
|
|
|
impl From<io::Error> for ParseError {
|
|
|
|
fn from(err: io::Error) -> ParseError {
|
2019-03-26 19:54:35 +01:00
|
|
|
ParseError::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<InvalidUri> for ParseError {
|
|
|
|
fn from(err: InvalidUri) -> ParseError {
|
|
|
|
ParseError::Uri(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Utf8Error> for ParseError {
|
|
|
|
fn from(err: Utf8Error) -> ParseError {
|
|
|
|
ParseError::Utf8(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FromUtf8Error> for ParseError {
|
|
|
|
fn from(err: FromUtf8Error) -> ParseError {
|
|
|
|
ParseError::Utf8(err.utf8_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<httparse::Error> for ParseError {
|
|
|
|
fn from(err: httparse::Error) -> ParseError {
|
|
|
|
match err {
|
|
|
|
httparse::Error::HeaderName
|
|
|
|
| httparse::Error::HeaderValue
|
|
|
|
| httparse::Error::NewLine
|
|
|
|
| httparse::Error::Token => ParseError::Header,
|
|
|
|
httparse::Error::Status => ParseError::Status,
|
|
|
|
httparse::Error::TooManyHeaders => ParseError::TooLarge,
|
|
|
|
httparse::Error::Version => ParseError::Version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
impl From<ParseError> for Error {
|
|
|
|
fn from(err: ParseError) -> Self {
|
|
|
|
Self::new_parse().with_cause(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 20:40:47 +01:00
|
|
|
impl From<ParseError> for Response<BoxBody> {
|
2021-06-17 18:57:58 +02:00
|
|
|
fn from(err: ParseError) -> Self {
|
|
|
|
Error::from(err).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 07:07:59 +02:00
|
|
|
/// A set of errors that can occur during payload parsing.
|
|
|
|
#[derive(Debug, Display)]
|
|
|
|
#[non_exhaustive]
|
2019-03-26 19:54:35 +01:00
|
|
|
pub enum PayloadError {
|
|
|
|
/// A payload reached EOF, but is not complete.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "payload reached EOF before completing: {:?}", _0)]
|
2019-03-26 19:54:35 +01:00
|
|
|
Incomplete(Option<io::Error>),
|
2021-01-09 14:17:19 +01:00
|
|
|
|
|
|
|
/// Content encoding stream corruption.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "can not decode content-encoding")]
|
2019-03-26 19:54:35 +01:00
|
|
|
EncodingCorrupted,
|
2021-01-09 14:17:19 +01:00
|
|
|
|
|
|
|
/// Payload reached size limit.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "payload reached size limit")]
|
2019-03-26 19:54:35 +01:00
|
|
|
Overflow,
|
2021-01-09 14:17:19 +01:00
|
|
|
|
|
|
|
/// Payload length is unknown.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "payload length is unknown")]
|
2019-03-26 19:54:35 +01:00
|
|
|
UnknownLength,
|
2021-01-09 14:17:19 +01:00
|
|
|
|
|
|
|
/// HTTP/2 payload error.
|
2022-01-31 22:22:23 +01:00
|
|
|
#[cfg(feature = "http2")]
|
2019-03-26 19:54:35 +01:00
|
|
|
#[display(fmt = "{}", _0)]
|
2022-01-31 22:22:23 +01:00
|
|
|
Http2Payload(::h2::Error),
|
2021-01-09 14:17:19 +01:00
|
|
|
|
|
|
|
/// Generic I/O error.
|
2019-03-28 19:08:24 +01:00
|
|
|
#[display(fmt = "{}", _0)]
|
|
|
|
Io(io::Error),
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:17:19 +01:00
|
|
|
impl std::error::Error for PayloadError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
PayloadError::Incomplete(None) => None,
|
2022-02-22 09:45:28 +01:00
|
|
|
PayloadError::Incomplete(Some(err)) => Some(err),
|
2021-01-09 14:17:19 +01:00
|
|
|
PayloadError::EncodingCorrupted => None,
|
|
|
|
PayloadError::Overflow => None,
|
|
|
|
PayloadError::UnknownLength => None,
|
2022-01-31 22:22:23 +01:00
|
|
|
#[cfg(feature = "http2")]
|
2022-02-22 09:45:28 +01:00
|
|
|
PayloadError::Http2Payload(err) => Some(err),
|
|
|
|
PayloadError::Io(err) => Some(err),
|
2021-01-09 14:17:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 02:51:06 +01:00
|
|
|
|
2022-01-31 22:22:23 +01:00
|
|
|
#[cfg(feature = "http2")]
|
|
|
|
impl From<::h2::Error> for PayloadError {
|
|
|
|
fn from(err: ::h2::Error) -> Self {
|
2019-03-28 19:08:24 +01:00
|
|
|
PayloadError::Http2Payload(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Option<io::Error>> for PayloadError {
|
|
|
|
fn from(err: Option<io::Error>) -> Self {
|
|
|
|
PayloadError::Incomplete(err)
|
|
|
|
}
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for PayloadError {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
PayloadError::Incomplete(Some(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
impl From<PayloadError> for Error {
|
|
|
|
fn from(err: PayloadError) -> Self {
|
|
|
|
Self::new_payload().with_cause(err)
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 07:07:59 +02:00
|
|
|
/// A set of errors that can occur during dispatching HTTP requests.
|
2021-12-17 02:29:41 +01:00
|
|
|
#[derive(Debug, Display, From)]
|
2022-02-03 08:03:39 +01:00
|
|
|
#[non_exhaustive]
|
2019-03-26 19:54:35 +01:00
|
|
|
pub enum DispatchError {
|
2021-12-17 02:29:41 +01:00
|
|
|
/// Service error.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "service error")]
|
2021-12-17 02:29:41 +01:00
|
|
|
Service(Response<BoxBody>),
|
2021-06-17 18:57:58 +02:00
|
|
|
|
2021-12-17 02:29:41 +01:00
|
|
|
/// Body streaming error.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "body error: {}", _0)]
|
2021-12-17 02:29:41 +01:00
|
|
|
Body(Box<dyn StdError>),
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2021-12-17 02:29:41 +01:00
|
|
|
/// Upgrade service error.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "upgrade error")]
|
2019-04-09 02:49:27 +02:00
|
|
|
Upgrade,
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
/// An `io::Error` that occurred while trying to read or write to a network stream.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "I/O error: {}", _0)]
|
2019-03-26 19:54:35 +01:00
|
|
|
Io(io::Error),
|
|
|
|
|
2021-12-17 02:29:41 +01:00
|
|
|
/// Request parse error.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "request parse error: {}", _0)]
|
2019-03-26 19:54:35 +01:00
|
|
|
Parse(ParseError),
|
|
|
|
|
2021-12-17 02:29:41 +01:00
|
|
|
/// HTTP/2 error.
|
2019-03-26 19:54:35 +01:00
|
|
|
#[display(fmt = "{}", _0)]
|
2022-01-31 22:22:23 +01:00
|
|
|
#[cfg(feature = "http2")]
|
2019-03-26 19:54:35 +01:00
|
|
|
H2(h2::Error),
|
|
|
|
|
|
|
|
/// The first request did not complete within the specified timeout.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "request did not complete within the specified timeout")]
|
2019-03-26 19:54:35 +01:00
|
|
|
SlowRequestTimeout,
|
|
|
|
|
2023-03-13 18:17:02 +01:00
|
|
|
/// Disconnect timeout. Makes sense for TLS streams.
|
|
|
|
#[display(fmt = "connection shutdown timeout")]
|
2019-03-26 19:54:35 +01:00
|
|
|
DisconnectTimeout,
|
|
|
|
|
2022-02-03 08:03:39 +01:00
|
|
|
/// Handler dropped payload before reading EOF.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "handler dropped payload before reading EOF")]
|
2022-02-03 08:03:39 +01:00
|
|
|
HandlerDroppedPayload,
|
|
|
|
|
2021-12-17 02:29:41 +01:00
|
|
|
/// Internal error.
|
2023-03-13 18:17:02 +01:00
|
|
|
#[display(fmt = "internal error")]
|
2019-03-26 19:54:35 +01:00
|
|
|
InternalError,
|
2021-12-17 02:29:41 +01:00
|
|
|
}
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2021-12-17 02:29:41 +01:00
|
|
|
impl StdError for DispatchError {
|
|
|
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
|
|
|
match self {
|
|
|
|
DispatchError::Service(_res) => None,
|
|
|
|
DispatchError::Body(err) => Some(&**err),
|
|
|
|
DispatchError::Io(err) => Some(err),
|
|
|
|
DispatchError::Parse(err) => Some(err),
|
2022-01-31 22:22:23 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "http2")]
|
2021-12-17 02:29:41 +01:00
|
|
|
DispatchError::H2(err) => Some(err),
|
2022-01-31 22:22:23 +01:00
|
|
|
|
2021-12-17 02:29:41 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 07:07:59 +02:00
|
|
|
/// A set of error that can occur during parsing content type.
|
|
|
|
#[derive(Debug, Display, Error)]
|
2022-07-23 17:26:48 +02:00
|
|
|
#[cfg_attr(test, derive(PartialEq, Eq))]
|
2021-04-14 07:07:59 +02:00
|
|
|
#[non_exhaustive]
|
2019-03-26 19:54:35 +01:00
|
|
|
pub enum ContentTypeError {
|
2023-03-13 18:17:02 +01:00
|
|
|
/// Can not parse content type.
|
|
|
|
#[display(fmt = "could not parse content type")]
|
2019-03-26 19:54:35 +01:00
|
|
|
ParseError,
|
2021-04-12 11:30:28 +02:00
|
|
|
|
2023-03-13 18:17:02 +01:00
|
|
|
/// Unknown content encoding.
|
|
|
|
#[display(fmt = "unknown content encoding")]
|
2019-03-26 19:54:35 +01:00
|
|
|
UnknownEncoding,
|
|
|
|
}
|
|
|
|
|
2021-04-14 07:07:59 +02:00
|
|
|
#[cfg(test)]
|
2022-01-19 03:09:25 +01:00
|
|
|
mod tests {
|
2024-02-19 13:31:10 +01:00
|
|
|
use http::Error as HttpError;
|
2021-04-14 07:07:59 +02:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_response() {
|
2021-12-04 20:40:47 +01:00
|
|
|
let resp: Response<BoxBody> = ParseError::Incomplete.into();
|
2019-03-26 19:54:35 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let err: HttpError = StatusCode::from_u16(10000).err().unwrap().into();
|
2021-12-04 20:40:47 +01:00
|
|
|
let resp: Response<BoxBody> = Error::new_http().with_cause(err).into();
|
2019-03-26 19:54:35 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_response() {
|
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
2021-06-17 18:57:58 +02:00
|
|
|
let err: Error = ParseError::Io(orig).into();
|
|
|
|
assert_eq!(
|
|
|
|
format!("{}", err),
|
2023-03-13 18:17:02 +01:00
|
|
|
"error parsing HTTP message: I/O error: other"
|
2021-06-17 18:57:58 +02:00
|
|
|
);
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_error_display() {
|
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
2021-06-17 18:57:58 +02:00
|
|
|
let err = Error::new_io().with_cause(orig);
|
|
|
|
assert_eq!("connection error: other", err.to_string());
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_error_http_response() {
|
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
2021-06-17 18:57:58 +02:00
|
|
|
let err = Error::new_io().with_cause(orig);
|
2021-12-04 20:40:47 +01:00
|
|
|
let resp: Response<BoxBody> = err.into();
|
2019-03-26 19:54:35 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
2019-03-28 13:04:39 +01:00
|
|
|
#[test]
|
|
|
|
fn test_payload_error() {
|
2021-12-08 07:01:11 +01:00
|
|
|
let err: PayloadError = io::Error::new(io::ErrorKind::Other, "ParseError").into();
|
2021-01-09 14:17:19 +01:00
|
|
|
assert!(err.to_string().contains("ParseError"));
|
2019-03-28 13:04:39 +01:00
|
|
|
|
2019-03-28 13:34:33 +01:00
|
|
|
let err = PayloadError::Incomplete(None);
|
2019-03-28 13:04:39 +01:00
|
|
|
assert_eq!(
|
2021-01-09 14:17:19 +01:00
|
|
|
err.to_string(),
|
2023-03-13 18:17:02 +01:00
|
|
|
"payload reached EOF before completing: None"
|
2019-03-28 13:04:39 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
macro_rules! from {
|
|
|
|
($from:expr => $error:pat) => {
|
|
|
|
match ParseError::from($from) {
|
2021-01-09 14:17:19 +01:00
|
|
|
err @ $error => {
|
|
|
|
assert!(err.to_string().len() >= 5);
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
2021-01-09 14:17:19 +01:00
|
|
|
err => unreachable!("{:?}", err),
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! from_and_cause {
|
|
|
|
($from:expr => $error:pat) => {
|
|
|
|
match ParseError::from($from) {
|
|
|
|
e @ $error => {
|
|
|
|
let desc = format!("{}", e);
|
2023-03-13 18:17:02 +01:00
|
|
|
assert_eq!(desc, format!("I/O error: {}", $from));
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
_ => unreachable!("{:?}", $from),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => ParseError::Io(..));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|