Skip to content

Commit

Permalink
http: derive Error for http::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwuelker committed Aug 11, 2024
1 parent 9da8b75 commit 559208f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
1 change: 1 addition & 0 deletions crates/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ url = { workspace = true }
dns = { workspace = true }
compression = { workspace = true }
log = { workspace = true }
error-derive = { workspace = true }
rustls = "0.22.2"
webpki-roots = "0.26.1"

Expand Down
54 changes: 23 additions & 31 deletions crates/http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use compression::{brotli, gzip, zlib};
use dns::DNSError;
use error_derive::Error;
use url::{Host, URL};

use crate::{https, response::Response, Header, Headers, StatusCode};
Expand All @@ -14,18 +15,39 @@ pub(crate) const HTTP_NEWLINE: &str = "\r\n";

const MAX_REDIRECTS: usize = 32;

#[derive(Debug)]
#[derive(Debug, Error)]
pub enum HTTPError {
#[msg = "invalid response"]
InvalidResponse,

#[msg = "status code indicates error"]
Status(StatusCode),

#[msg = "io error"]
IO(io::Error),

#[msg = "failed to resolve host"]
DNS(DNSError),

#[msg = "gzip decompression failed"]
Gzip(gzip::Error),

#[msg = "brotli decompression failed"]
Brotli(brotli::Error),

#[msg = "zlib decompression failed"]
Zlib(zlib::Error),

#[msg = "tls communication failed"]
Tls(rustls::Error),

#[msg = "too many redirections"]
RedirectLoop,

#[msg = "redirect to non-http url"]
NonHTTPRedirect,

#[msg = "request to non-http url"]
NonHTTPURl,
}

Expand Down Expand Up @@ -270,33 +292,3 @@ impl Request {
Ok(response)
}
}

impl From<io::Error> for HTTPError {
fn from(value: io::Error) -> Self {
Self::IO(value)
}
}

impl From<gzip::Error> for HTTPError {
fn from(value: gzip::Error) -> Self {
Self::Gzip(value)
}
}

impl From<brotli::Error> for HTTPError {
fn from(value: brotli::Error) -> Self {
Self::Brotli(value)
}
}

impl From<zlib::Error> for HTTPError {
fn from(value: zlib::Error) -> Self {
Self::Zlib(value)
}
}

impl From<rustls::Error> for HTTPError {
fn from(value: rustls::Error) -> Self {
Self::Tls(value)
}
}

0 comments on commit 559208f

Please sign in to comment.