pub trait HttpError:
Error
+ Send
+ Sync
+ 'static {
// Required method
fn status(&self) -> Option<StatusCode>;
// Provided method
fn is_remote(&self) -> bool { ... }
}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.
Required Methods§
Sourcefn status(&self) -> Option<StatusCode>
fn status(&self) -> Option<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) -> Option<StatusCode> {
Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
let err = MyError;
assert_eq!(err.status(), Some(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(), Some(StatusCode::BAD_GATEWAY));