Skip to content

Commit

Permalink
refactor: remove error status reference
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Apr 2, 2023
1 parent c67edc7 commit 8e811bb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
54 changes: 27 additions & 27 deletions src/error/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,143 +15,143 @@ macro_rules! bail {
}

// 4xx
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::BAD_REQUEST`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::BAD_REQUEST`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_bad_request {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::BAD_REQUEST)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::UNAUTHORIZED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::UNAUTHORIZED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_unauthorized {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::UNAUTHORIZED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PAYMENT_REQUIRED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PAYMENT_REQUIRED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_payment_required {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::PAYMENT_REQUIRED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::FORBIDDEN`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::FORBIDDEN`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_forbidden {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::FORBIDDEN)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::NOT_FOUND`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_FOUND`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_not_found {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::NOT_FOUND)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::METHOD_NOT_ALLOWED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::METHOD_NOT_ALLOWED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_method_not_allowed {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::METHOD_NOT_ALLOWED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::NOT_ACCEPTABLE`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_ACCEPTABLE`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_not_acceptable {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::NOT_ACCEPTABLE)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PROXY_AUTHENTICATION_REQUIRED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PROXY_AUTHENTICATION_REQUIRED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_proxy_authentication_required {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::PROXY_AUTHENTICATION_REQUIRED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::REQUEST_TIMEOUT`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::REQUEST_TIMEOUT`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_request_timeout {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::REQUEST_TIMEOUT)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::CONFLICT`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::CONFLICT`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_conflict {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::CONFLICT)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::GONE`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::GONE`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_gone {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::GONE)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::LENGTH_REQUIRED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::LENGTH_REQUIRED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_length_required {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::LENGTH_REQUIRED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PRECONDITION_FAILED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PRECONDITION_FAILED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_precondition_failed {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::PRECONDITION_FAILED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PAYLOAD_TOO_LARGE`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PAYLOAD_TOO_LARGE`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_payload_too_large {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::PAYLOAD_TOO_LARGE)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::URI_TOO_LONG`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::URI_TOO_LONG`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_uri_too_long {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::URI_TOO_LONG)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::UNSUPPORTED_MEDIA_TYPE`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::UNSUPPORTED_MEDIA_TYPE`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_unsupported_media_type {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::UNSUPPORTED_MEDIA_TYPE)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::RANGE_NOT_SATISFIABLE`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::RANGE_NOT_SATISFIABLE`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_range_not_satisfiable {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::RANGE_NOT_SATISFIABLE)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::EXPECTATION_FAILED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::EXPECTATION_FAILED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_expectation_failed {
($($arg:tt)*) => {{
Expand All @@ -160,71 +160,71 @@ macro_rules! http_error_expectation_failed {
}

// 50x
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::INTERNAL_SERVER_ERROR`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::INTERNAL_SERVER_ERROR`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_internal_server_error {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::INTERNAL_SERVER_ERROR)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::NOT_IMPLEMENTED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_IMPLEMENTED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_not_implemented {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::NOT_IMPLEMENTED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::BAD_GATEWAY`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::BAD_GATEWAY`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_bad_gateway {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::BAD_GATEWAY)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::SERVICE_UNAVAILABLE`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::SERVICE_UNAVAILABLE`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_service_unavailable {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::SERVICE_UNAVAILABLE)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::GATEWAY_TIMEOUT`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::GATEWAY_TIMEOUT`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_gateway_timeout {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::GATEWAY_TIMEOUT)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::HTTP_VERSION_NOT_SUPPORTED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::HTTP_VERSION_NOT_SUPPORTED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_http_version_not_supported {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::HTTP_VERSION_NOT_SUPPORTED)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::VARIANT_ALSO_NEGOTIATES`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::VARIANT_ALSO_NEGOTIATES`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_variant_also_negotiates {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::VARIANT_ALSO_NEGOTIATES)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::INSUFFICIENT_STORAGE`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::INSUFFICIENT_STORAGE`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_insufficient_storage {
($($arg:tt)*) => {{
$crate::error!($($arg)*).with_status(StatusCode::INSUFFICIENT_STORAGE)
}}
}

/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::LOOP_DETECTED`] from a string or existing non-anyhow error value.
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::LOOP_DETECTED`] from a string or existing non-anyhow error value.
#[macro_export]
macro_rules! http_error_loop_detected {
($($arg:tt)*) => {{
Expand Down
30 changes: 26 additions & 4 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@
//! The `Error` type implements other several common error types
//! to ease conversions while consuming the input value via the [`From`] trait.
//!
//! Additionally, when used in HTTP contexts, the `Error` type can be associated to an [HTTP Status Code][`http::StatusCode`]
//! Additionally, when used in HTTP contexts, the `Error` type can be associated to an [HTTP Status Code][`hyper::StatusCode`].
//! via the [`Error::with_status`][`super::Error::with_status`] method.
//!
//! a. Construct an [`Error`][`super::Error`] from [`hyper::Error`], [`std::io::Error`], [`anyhow::Error`] or an string.
//!
//! ```rust
//! use hyper_middleware::error
//!
//! let err = Error::from("some error type or string");
//! // Or using a shortcut macro
//! let err = error!("some error type or string");
//! ```
//!
//! b. Construct an [`Error`][`super::Error`] with an associated [HTTP Status Code][`hyper::StatusCode`].
//!
//! ```rust
//! use hyper::StatusCode;
//! use hyper_middleware::error
//!
//! let err = error!("user or password does not match").with_status(StatusCode::UNAUTHORIZED);
//! // Or using a shortcut macro
//! let err = http_error_unauthorized!("user or password does not match");
//! ```
//!
use hyper::StatusCode;
use std::fmt;
Expand All @@ -29,9 +51,9 @@ impl Error {
self.source
}

/// Returns an HTTP Status Code reference associated with the underlying error.
pub fn status(&self) -> Option<&StatusCode> {
self.status.as_ref()
/// Returns the HTTP `StatusCode` associated with the underlying error.
pub fn status(&self) -> Option<StatusCode> {
self.status
}

/// Adds/updates the current HTTP Status Code.
Expand Down

0 comments on commit 8e811bb

Please # to comment.