use std::{error::Error as StdError, fmt};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Class {
Request,
Authentication,
Connect,
Timeout,
Redirect,
Runtime,
Body,
BodyTooLarge,
Decode,
}
#[derive(Clone)]
pub struct Error {
class: Class,
message: &'static str,
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub const fn request(message: &'static str) -> Self {
Self {
class: Class::Request,
message,
}
}
pub(crate) const fn connect() -> Self {
Self {
class: Class::Connect,
message: "could not establish an HTTP connection",
}
}
pub const fn authentication() -> Self {
Self {
class: Class::Authentication,
message: "request authentication could not be obtained",
}
}
pub(crate) const fn timeout() -> Self {
Self {
class: Class::Timeout,
message: "HTTP request deadline exceeded",
}
}
pub(crate) const fn redirect() -> Self {
Self {
class: Class::Redirect,
message: "HTTP redirect could not be completed",
}
}
pub(crate) const fn runtime() -> Self {
Self {
class: Class::Runtime,
message: "an asynchronous I/O and time runtime is required to send HTTP requests",
}
}
pub(crate) const fn body() -> Self {
Self {
class: Class::Body,
message: "HTTP body could not be read or written",
}
}
pub(crate) const fn body_too_large() -> Self {
Self {
class: Class::BodyTooLarge,
message: "HTTP body exceeds the 16 MiB collection limit",
}
}
pub(crate) const fn decode(message: &'static str) -> Self {
Self {
class: Class::Decode,
message,
}
}
#[must_use]
pub fn is_request(&self) -> bool {
self.class == Class::Request
}
#[must_use]
pub fn is_authentication(&self) -> bool {
self.class == Class::Authentication
}
#[must_use]
pub fn is_connect(&self) -> bool {
self.class == Class::Connect
}
#[must_use]
pub fn is_timeout(&self) -> bool {
self.class == Class::Timeout
}
#[must_use]
pub fn is_redirect(&self) -> bool {
self.class == Class::Redirect
}
#[must_use]
pub fn is_runtime(&self) -> bool {
self.class == Class::Runtime
}
#[must_use]
pub fn is_body(&self) -> bool {
matches!(self.class, Class::Body | Class::BodyTooLarge)
}
#[must_use]
pub fn is_body_too_large(&self) -> bool {
self.class == Class::BodyTooLarge
}
#[must_use]
pub fn is_decode(&self) -> bool {
self.class == Class::Decode
}
}
impl From<http::Error> for Error {
fn from(_: http::Error) -> Self {
Self::request("HTTP request could not be constructed")
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.message)
}
}
impl fmt::Debug for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Error")
.field("class", &self.class)
.field("message", &self.message)
.finish()
}
}
impl StdError for Error {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn predicates_expose_only_the_stable_error_categories() {
assert!(Error::request("invalid").is_request());
assert!(Error::authentication().is_authentication());
assert!(Error::connect().is_connect());
assert!(Error::timeout().is_timeout());
assert!(Error::redirect().is_redirect());
assert!(Error::runtime().is_runtime());
assert!(Error::body().is_body());
assert!(Error::decode("invalid").is_decode());
let too_large = Error::body_too_large();
assert!(too_large.is_body());
assert!(too_large.is_body_too_large());
}
#[test]
fn formatting_does_not_expose_callers_input() {
let error = Error::request("request URL is invalid");
assert!(!error.to_string().contains("secret"));
assert!(!format!("{error:?}").contains("secret"));
}
#[test]
fn runtime_error_does_not_name_the_private_runtime() {
assert!(!Error::runtime().to_string().contains("Tokio"));
}
}