pub struct HttpError { /* private fields */ }
Expand description
HttpError
is an error that encapsulates data to generate Http error responses.
Implementations§
Source§impl HttpError
impl HttpError
Sourcepub const fn from_static(status_code: StatusCode, reason: &'static str) -> Self
pub const fn from_static(status_code: StatusCode, reason: &'static str) -> Self
Creates a HttpError
with status code and reason. This constructor can be evaluated at
compile time.
use anyhow_http::HttpError;
const BAD_REQUEST: HttpError =
HttpError::from_static(http::StatusCode::BAD_REQUEST, "invalid request");
Sourcepub const fn from_status_code(status_code: StatusCode) -> Self
pub const fn from_status_code(status_code: StatusCode) -> Self
Creates a HttpError
from a status code.
Sourcepub const fn with_status_code(self, status_code: StatusCode) -> Self
pub const fn with_status_code(self, status_code: StatusCode) -> Self
Sets the status code.
Sourcepub fn with_reason<S: Into<Cow<'static, str>>>(self, reason: S) -> Self
pub fn with_reason<S: Into<Cow<'static, str>>>(self, reason: S) -> Self
Sets the error reason.
Sourcepub fn with_source_context<C>(self, context: C) -> Self
pub fn with_source_context<C>(self, context: C) -> Self
Adds context to the source error. If no source is availabe a new anyhow::Error
is
created in its place.
Sourcepub fn with_boxed_source_err(
self,
err: Box<dyn StdError + Send + Sync + 'static>,
) -> Self
pub fn with_boxed_source_err( self, err: Box<dyn StdError + Send + Sync + 'static>, ) -> Self
Set the source error from a generic error trait object.
Sourcepub fn with_source_err<E>(self, err: E) -> Self
pub fn with_source_err<E>(self, err: E) -> Self
Set the source error from a generic error.
Sourcepub fn with_data<I, K, V>(self, values: I) -> Option<Self>
pub fn with_data<I, K, V>(self, values: I) -> Option<Self>
Append to to the inner data based on one or more key-value pairs.
use anyhow_http::HttpError;
let err: HttpError = HttpError::default()
.with_data([("key1", 1234), ("key2", 5678)])
.unwrap();
Sourcepub fn with_key_value<K, V>(self, key: K, value: V) -> Self
pub fn with_key_value<K, V>(self, key: K, value: V) -> Self
Adds a key-pair value to the inner data.
pub fn with_header<K>(self, header_key: K, header_value: HeaderValue) -> Selfwhere
K: IntoHeaderName,
Sourcepub fn get<V>(&self, key: impl AsRef<str>) -> Option<V>
pub fn get<V>(&self, key: impl AsRef<str>) -> Option<V>
Retrieves a key-pair value from the inner data.
Sourcepub fn set<K, V>(&mut self, key: K, value: V) -> Result<()>
pub fn set<K, V>(&mut self, key: K, value: V) -> Result<()>
Sets a key-pair value pair into the inner data.
Sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
Returns the status code.
pub fn into_boxed(self) -> Box<dyn StdError + Send + Sync + 'static>
Trait Implementations§
Source§impl Error for HttpError
impl Error for HttpError
Source§fn source(&self) -> Option<&(dyn StdError + 'static)>
fn source(&self) -> Option<&(dyn StdError + '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()
Auto Trait Implementations§
impl Freeze for HttpError
impl RefUnwindSafe for HttpError
impl Send for HttpError
impl Sync for HttpError
impl Unpin for HttpError
impl UnwindSafe for HttpError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more