use std::{error::Error as StdError, fmt};
use http::StatusCode;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Class {
Request,
Authentication,
Connect,
Timeout,
Redirect,
Body,
BodyTooLarge,
Decode,
Upgrade,
Status(StatusCode),
}
#[derive(Clone)]
pub struct Error {
class: Class,
message: &'static str,
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
#[must_use]
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",
}
}
#[must_use]
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 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 its collection limit",
}
}
pub(crate) const fn decode(message: &'static str) -> Self {
Self {
class: Class::Decode,
message,
}
}
pub(crate) const fn upgrade() -> Self {
Self {
class: Class::Upgrade,
message: "HTTP protocol upgrade could not be completed",
}
}
pub(crate) const fn status_error(status: StatusCode) -> Self {
Self {
class: Class::Status(status),
message: "HTTP response status indicates an error",
}
}
#[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_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
}
#[must_use]
pub fn is_upgrade(&self) -> bool {
self.class == Class::Upgrade
}
#[must_use]
pub fn is_status(&self) -> bool {
matches!(self.class, Class::Status(_))
}
#[must_use]
pub const fn status(&self) -> Option<StatusCode> {
match self.class {
Class::Status(status) => Some(status),
_ => None,
}
}
}
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::body().is_body());
assert!(Error::decode("invalid").is_decode());
assert!(Error::upgrade().is_upgrade());
let status = Error::status_error(StatusCode::BAD_REQUEST);
assert!(status.is_status());
assert_eq!(status.status(), Some(StatusCode::BAD_REQUEST));
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"));
}
}