2018-11-12 08:12:54 +01:00
|
|
|
use std::io;
|
|
|
|
|
2019-12-05 18:35:43 +01:00
|
|
|
use actix_connect::resolver::ResolveError;
|
2018-12-20 03:34:56 +01:00
|
|
|
use derive_more::{Display, From};
|
2018-11-12 08:12:54 +01:00
|
|
|
|
2019-11-19 13:54:19 +01:00
|
|
|
#[cfg(feature = "openssl")]
|
2019-12-05 18:35:43 +01:00
|
|
|
use actix_connect::ssl::openssl::{HandshakeError, SslError};
|
2018-11-12 08:12:54 +01:00
|
|
|
|
2019-01-29 05:41:09 +01:00
|
|
|
use crate::error::{Error, ParseError, ResponseError};
|
2019-11-26 11:07:39 +01:00
|
|
|
use crate::http::{Error as HttpError, StatusCode};
|
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
|
|
|
|
2019-12-05 18:35:43 +01:00
|
|
|
/// SSL Handshake error
|
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
#[display(fmt = "{}", _0)]
|
|
|
|
SslHandshakeError(String),
|
|
|
|
|
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-12-05 18:35:43 +01:00
|
|
|
impl<T: std::fmt::Debug> From<HandshakeError<T>> for ConnectError {
|
2019-03-13 22:41:40 +01:00
|
|
|
fn from(err: HandshakeError<T>) -> ConnectError {
|
2019-12-05 18:35:43 +01:00
|
|
|
ConnectError::SslHandshakeError(format!("{:?}", err))
|
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 {
|
2019-11-26 11:07:39 +01:00
|
|
|
fn status_code(&self) -> StatusCode {
|
2019-01-29 05:41:09 +01:00
|
|
|
match *self {
|
2019-03-13 22:41:40 +01:00
|
|
|
SendRequestError::Connect(ConnectError::Timeout) => {
|
2019-11-26 11:07:39 +01:00
|
|
|
StatusCode::GATEWAY_TIMEOUT
|
2019-01-29 05:41:09 +01:00
|
|
|
}
|
2019-11-26 11:07:39 +01:00
|
|
|
SendRequestError::Connect(_) => StatusCode::BAD_REQUEST,
|
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
2019-01-29 05:41:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 06:29:32 +02:00
|
|
|
|
|
|
|
/// 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
|
|
|
}
|