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);

Implementations on Foreign Types§

Source§

impl HttpError for Infallible

Implementors§