Struct HttpError

Source
pub struct HttpError { /* private fields */ }
Expand description

HttpError is an error that encapsulates data to generate Http error responses.

Implementations§

Source§

impl HttpError

Source

pub const fn new() -> Self

Creates an empty HttpError with status 500.

Source

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

pub const fn from_status_code(status_code: StatusCode) -> Self

Creates a HttpError from a status code.

Source

pub const fn with_status_code(self, status_code: StatusCode) -> Self

Sets the status code.

Source

pub fn with_reason<S: Into<Cow<'static, str>>>(self, reason: S) -> Self

Sets the error reason.

Source

pub fn with_source_context<C>(self, context: C) -> Self
where C: Display + Send + Sync + 'static,

Adds context to the source error. If no source is availabe a new anyhow::Error is created in its place.

Source

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.

Source

pub fn with_source_err<E>(self, err: E) -> Self
where E: Into<Error>,

Set the source error from a generic error.

Source

pub fn with_data<I, K, V>(self, values: I) -> Option<Self>
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Serialize + Sync + Send + 'static,

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

pub fn with_key_value<K, V>(self, key: K, value: V) -> Self
where K: Into<String>, V: Serialize + Sync + Send + 'static,

Adds a key-pair value to the inner data.

Source

pub fn with_header<K>(self, header_key: K, header_value: HeaderValue) -> Self
where K: IntoHeaderName,

Source

pub fn get<V>(&self, key: impl AsRef<str>) -> Option<V>
where V: DeserializeOwned + Send + Sync,

Retrieves a key-pair value from the inner data.

Source

pub fn set<K, V>(&mut self, key: K, value: V) -> Result<()>
where K: Into<String>, V: Serialize + Sync + Send + 'static,

Sets a key-pair value pair into the inner data.

Source

pub fn status_code(&self) -> StatusCode

Returns the status code.

Source

pub fn reason(&self) -> Option<Cow<'static, str>>

Returns the error reason if any.

Source

pub fn source(&self) -> Option<&Error>

Returns the source error if any.

Source

pub fn headers(&self) -> Option<&HeaderMap>

Returns the error response headers if any.

Source

pub fn from_err<E>(err: E) -> Self
where E: Into<Error>,

Creates a HttpError from a generic error. It attempts to downcast to an underlying HttpError.

Source

pub fn into_boxed(self) -> Box<dyn StdError + Send + Sync + 'static>

Trait Implementations§

Source§

impl Debug for HttpError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HttpError

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for HttpError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for HttpError

Source§

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

👎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 From<Error> for HttpError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for HttpError

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.