HttpError

Trait HttpError 

Source
pub trait HttpError:
    Error
    + Send
    + Sync
    + 'static {
    // Provided method
    fn status(&self) -> StatusCode { ... }
}
Expand description

Trait for errors that have an associated HTTP status code.

This trait extends the standard Error trait to include a method for retrieving the HTTP status code associated with the error.

Provided Methods§

Source

fn status(&self) -> StatusCode

Returns the HTTP status code associated with this error.

§Examples
use http_kit::{HttpError,StatusCode};
use thiserror::Error;

#[derive(Debug, Error)]
#[error("My error occurred")]
struct MyError;

impl HttpError for MyError {
    fn status(&self) -> StatusCode {
        StatusCode::INTERNAL_SERVER_ERROR
    }
}
let err = MyError;
assert_eq!(err.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(err.to_string(), "My error occurred");

Alternatively, you can use the http_error! macro to build zero-sized types that already implement HttpError with a fixed status code:

use http_kit::{http_error, StatusCode, HttpError};

http_error!(pub BadGateway, StatusCode::BAD_GATEWAY, "upstream failed");
let err = BadGateway::new();
assert_eq!(err.status(), StatusCode::BAD_GATEWAY);

Trait Implementations§

Source§

impl Error for Box<dyn HttpError>

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl HttpError for Box<dyn HttpError>

Source§

fn status(&self) -> StatusCode

Returns the HTTP status code associated with this error. Read more

Implementations on Foreign Types§

Source§

impl HttpError for Infallible

Source§

impl HttpError for Box<dyn HttpError>

Implementors§

Source§

impl<A, B> HttpError for MiddlewareTupleError<A, B>
where A: HttpError, B: HttpError,

Source§

impl<N, E> HttpError for MiddlewareError<N, E>
where N: HttpError, E: HttpError,