1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 08:52:42 +01:00
actix-web/actix-http/src/client/error.rs

151 lines
4.4 KiB
Rust
Raw Normal View History

2018-11-12 08:12:54 +01:00
use std::io;
2018-12-20 03:34:56 +01:00
use derive_more::{Display, From};
2018-11-12 08:12:54 +01:00
use trust_dns_resolver::error::ResolveError;
2019-11-19 13:54:19 +01:00
#[cfg(feature = "openssl")]
use open_ssl::ssl::{Error as SslError, HandshakeError};
2018-11-12 08:12:54 +01:00
2019-01-29 05:41:09 +01:00
use crate::error::{Error, ParseError, ResponseError};
2019-03-26 05:52:45 +01:00
use crate::http::Error as HttpError;
2019-01-29 05:41:09 +01:00
use crate::response::Response;
2018-11-14 07:53:30 +01:00
2018-11-12 08:12:54 +01:00
/// A set of errors that can occur while connecting to an HTTP host
2018-12-20 03:34:56 +01:00
#[derive(Debug, Display, From)]
2019-03-13 22:41:40 +01:00
pub enum ConnectError {
2018-11-12 08:12:54 +01:00
/// SSL feature is not enabled
2018-12-20 03:34:56 +01:00
#[display(fmt = "SSL is not supported")]
2018-11-12 08:12:54 +01:00
SslIsNotSupported,
/// SSL error
2019-11-19 13:54:19 +01:00
#[cfg(feature = "openssl")]
2018-12-20 03:34:56 +01:00
#[display(fmt = "{}", _0)]
SslError(SslError),
2018-11-12 08:12:54 +01:00
/// Failed to resolve the hostname
2018-12-20 03:34:56 +01:00
#[display(fmt = "Failed resolving hostname: {}", _0)]
2018-11-12 08:12:54 +01:00
Resolver(ResolveError),
/// No dns records
2018-12-20 03:34:56 +01:00
#[display(fmt = "No dns records found for the input")]
2018-11-12 08:12:54 +01:00
NoRecords,
2019-01-29 05:41:09 +01:00
/// Http2 error
#[display(fmt = "{}", _0)]
H2(h2::Error),
2018-11-12 08:12:54 +01:00
/// Connecting took too long
2018-12-20 03:34:56 +01:00
#[display(fmt = "Timeout out while establishing connection")]
2018-11-12 08:12:54 +01:00
Timeout,
/// Connector has been disconnected
2018-12-20 03:34:56 +01:00
#[display(fmt = "Internal error: connector has been disconnected")]
2018-11-12 08:12:54 +01:00
Disconnected,
2019-03-13 22:41:40 +01:00
/// Unresolved host name
#[display(fmt = "Connector received `Connect` method with unresolved host")]
Unresolverd,
2018-11-12 08:12:54 +01:00
/// Connection io error
2018-12-20 03:34:56 +01:00
#[display(fmt = "{}", _0)]
Io(io::Error),
2018-11-12 08:12:54 +01:00
}
2019-03-13 22:41:40 +01:00
impl From<actix_connect::ConnectError> for ConnectError {
fn from(err: actix_connect::ConnectError) -> ConnectError {
match err {
actix_connect::ConnectError::Resolver(e) => ConnectError::Resolver(e),
actix_connect::ConnectError::NoRecords => ConnectError::NoRecords,
actix_connect::ConnectError::InvalidInput => panic!(),
actix_connect::ConnectError::Unresolverd => ConnectError::Unresolverd,
actix_connect::ConnectError::Io(e) => ConnectError::Io(e),
}
}
2018-11-12 08:12:54 +01:00
}
2019-11-19 13:54:19 +01:00
#[cfg(feature = "openssl")]
2019-03-13 22:41:40 +01:00
impl<T> From<HandshakeError<T>> for ConnectError {
fn from(err: HandshakeError<T>) -> ConnectError {
2018-12-11 03:08:33 +01:00
match err {
HandshakeError::SetupFailure(stack) => SslError::from(stack).into(),
2019-01-29 19:14:00 +01:00
HandshakeError::Failure(stream) => stream.into_error().into(),
HandshakeError::WouldBlock(stream) => stream.into_error().into(),
2018-12-11 03:08:33 +01:00
}
}
}
2019-03-13 22:41:40 +01:00
#[derive(Debug, Display, From)]
pub enum InvalidUrl {
#[display(fmt = "Missing url scheme")]
MissingScheme,
#[display(fmt = "Unknown url scheme")]
UnknownScheme,
#[display(fmt = "Missing host name")]
MissingHost,
#[display(fmt = "Url parse error: {}", _0)]
HttpError(http::Error),
}
2018-11-14 07:53:30 +01:00
/// A set of errors that can occur during request sending and response reading
2018-12-20 03:34:56 +01:00
#[derive(Debug, Display, From)]
2018-11-14 07:53:30 +01:00
pub enum SendRequestError {
2019-03-13 22:41:40 +01:00
/// Invalid URL
#[display(fmt = "Invalid URL: {}", _0)]
Url(InvalidUrl),
2018-11-14 07:53:30 +01:00
/// Failed to connect to host
2018-12-20 03:34:56 +01:00
#[display(fmt = "Failed to connect to host: {}", _0)]
2019-03-13 22:41:40 +01:00
Connect(ConnectError),
2018-11-14 07:53:30 +01:00
/// Error sending request
Send(io::Error),
/// Error parsing response
Response(ParseError),
2019-03-26 05:52:45 +01:00
/// Http error
#[display(fmt = "{}", _0)]
Http(HttpError),
2019-01-29 05:41:09 +01:00
/// Http2 error
#[display(fmt = "{}", _0)]
H2(h2::Error),
2019-03-29 06:33:41 +01:00
/// Response took too long
#[display(fmt = "Timeout out while waiting for response")]
Timeout,
2019-03-28 02:53:19 +01:00
/// Tunnels are not supported for http2 connection
#[display(fmt = "Tunnels are not supported for http2 connection")]
TunnelNotSupported,
2018-11-14 07:53:30 +01:00
/// Error sending request body
Body(Error),
}
2019-01-29 05:41:09 +01:00
/// Convert `SendRequestError` to a server `Response`
impl ResponseError for SendRequestError {
fn error_response(&self) -> Response {
match *self {
2019-03-13 22:41:40 +01:00
SendRequestError::Connect(ConnectError::Timeout) => {
2019-01-29 05:41:09 +01:00
Response::GatewayTimeout()
}
2019-03-13 22:41:40 +01:00
SendRequestError::Connect(_) => Response::BadGateway(),
2019-01-29 05:41:09 +01:00
_ => Response::InternalServerError(),
}
.into()
}
}
/// A set of errors that can occur during freezing a request
#[derive(Debug, Display, From)]
pub enum FreezeRequestError {
/// Invalid URL
#[display(fmt = "Invalid URL: {}", _0)]
Url(InvalidUrl),
/// Http error
#[display(fmt = "{}", _0)]
Http(HttpError),
}
impl From<FreezeRequestError> for SendRequestError {
fn from(e: FreezeRequestError) -> Self {
match e {
FreezeRequestError::Url(e) => e.into(),
FreezeRequestError::Http(e) => e.into(),
}
}
2019-09-12 17:52:46 +02:00
}