This commit is contained in:
Martin Algesten
2018-06-16 11:01:28 +02:00
parent ca2e193c84
commit 4791db3a63
4 changed files with 51 additions and 20 deletions

View File

@@ -3,28 +3,41 @@ use native_tls::Error as TlsError;
use native_tls::HandshakeError;
use std::net::TcpStream;
/// Errors that are translated to ["synthetic" responses](struct.Response.html#method.synthetic).
#[derive(Debug)]
pub enum Error {
/// The url could not be understood. Synthetic error `400`.
BadUrl(String),
/// The url scheme could not be understood. Synthetic error `400`.
UnknownScheme(String),
/// DNS lookup failed. Synthetic error `400`.
DnsFailed(String),
/// Connection to server failed. Synthetic error `500`.
ConnectionFailed(String),
/// Too many redirects. Synthetic error `500`.
TooManyRedirects,
/// A status line we don't understand `HTTP/1.1 200 OK`. Synthetic error `500`.
BadStatus,
/// A header line that couldn't be parsed. Synthetic error `500`.
BadHeader,
/// Some unspecified `std::io::Error`. Synthetic error `500`.
Io(IoError),
/// Some unspecified TLS error. Synthetic error `400`.
Tls(TlsError),
/// Some unspecified TLS handshake error. Synthetic error `500`.
TlsHandshake(HandshakeError<TcpStream>),
}
impl Error {
/// For synthetic responses, this is the error code.
pub fn status(&self) -> u16 {
match self {
Error::BadUrl(_) => 400,
Error::UnknownScheme(_) => 400,
Error::DnsFailed(_) => 400,
Error::ConnectionFailed(_) => 500,
Error::TooManyRedirects => 400,
Error::TooManyRedirects => 500,
Error::BadStatus => 500,
Error::BadHeader => 500,
Error::Io(_) => 500,
@@ -32,6 +45,8 @@ impl Error {
Error::TlsHandshake(_) => 500,
}
}
/// For synthetic responses, this is the status text.
pub fn status_text(&self) -> &str {
match self {
Error::BadUrl(e) => {
@@ -49,6 +64,8 @@ impl Error {
Error::TlsHandshake(_) => "TLS Handshake Error",
}
}
/// For synthetic responses, this is the body text.
pub fn body_text(&self) -> String {
match self {
Error::BadUrl(url) => format!("Bad URL: {}", url),