From 559208f903fc893738ddf1cb8abc984a406115c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Sun, 11 Aug 2024 16:16:02 +0200 Subject: [PATCH] http: derive Error for http::Error --- crates/http/Cargo.toml | 1 + crates/http/src/request.rs | 54 ++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/crates/http/Cargo.toml b/crates/http/Cargo.toml index b4233793..0fad78d1 100644 --- a/crates/http/Cargo.toml +++ b/crates/http/Cargo.toml @@ -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" diff --git a/crates/http/src/request.rs b/crates/http/src/request.rs index 55638368..8a69f0cd 100644 --- a/crates/http/src/request.rs +++ b/crates/http/src/request.rs @@ -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}; @@ -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, } @@ -270,33 +292,3 @@ impl Request { Ok(response) } } - -impl From for HTTPError { - fn from(value: io::Error) -> Self { - Self::IO(value) - } -} - -impl From for HTTPError { - fn from(value: gzip::Error) -> Self { - Self::Gzip(value) - } -} - -impl From for HTTPError { - fn from(value: brotli::Error) -> Self { - Self::Brotli(value) - } -} - -impl From for HTTPError { - fn from(value: zlib::Error) -> Self { - Self::Zlib(value) - } -} - -impl From for HTTPError { - fn from(value: rustls::Error) -> Self { - Self::Tls(value) - } -}