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§
Sourcefn status(&self) -> StatusCode
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>
impl Error for Box<dyn HttpError>
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()