Struct Error

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

The main error type for HTTP operations.

This error type wraps any error with an associated HTTP status code, providing both the underlying error information and the appropriate HTTP response status.

§Examples

use http_kit::Error;
use http::StatusCode;

// Create from a string message
let err = Error::msg("Something went wrong");

// Create with a specific status code
let err = Error::msg("Not found").set_status(StatusCode::NOT_FOUND);

Implementations§

Source§

impl Error

Source

pub fn new<E, S>(error: E, status: S) -> Self
where E: Into<Error>, S: TryInto<StatusCode>, S::Error: Debug,

Creates a new Error from any error type with the given HTTP status code.

§Arguments
  • error - Any error type that can be converted to anyhow::Error
  • status - HTTP status code (or value convertible to one)
§Panics

Panics if the status code is invalid.

§Examples
use http_kit::Error;
use http::StatusCode;
use std::io;

let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let http_err = Error::new(io_err, StatusCode::NOT_FOUND);
Source

pub fn msg<S>(msg: S) -> Self
where S: Display + Debug + Send + Sync + 'static,

Creates an Error from a message string with a default status code.

The default status code is SERVICE_UNAVAILABLE (503).

§Arguments
  • msg - Any type that implements Display + Debug + Send + Sync + 'static
§Examples
use http_kit::Error;

let err = Error::msg("Something went wrong");
let err = Error::msg(format!("Failed to process item {}", 42));
Source

pub fn set_status<S>(self, status: S) -> Self
where S: TryInto<StatusCode>, S::Error: Debug,

Sets the HTTP status code of this error.

Only error status codes (400-599) can be set. In debug builds, this method will assert that the status code is in the valid range.

§Arguments
  • status - HTTP status code (or value convertible to one)
§Panics

Panics if the status code is invalid or not an error status code.

§Examples
use http_kit::Error;
use http::StatusCode;

let err = Error::msg("Not found").set_status(StatusCode::NOT_FOUND);
Source

pub fn status(&self) -> StatusCode

Returns the HTTP status code associated with this error.

§Examples
use http_kit::Error;
use http::StatusCode;

let err = Error::msg("not found").set_status(StatusCode::NOT_FOUND);
assert_eq!(err.status(), StatusCode::NOT_FOUND);
Source

pub fn downcast<E>(self) -> Result<Box<E>, Self>
where E: Error + Send + Sync + 'static,

Attempts to downcast the inner error to a concrete type.

Returns Ok(Box<E>) if the downcast succeeds, or Err(Self) if it fails.

§Examples
use http_kit::Error;
use http::StatusCode;
use std::io;

let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let http_err = Error::new(io_err, StatusCode::NOT_FOUND);

match http_err.downcast::<io::Error>() {
    Ok(io_error) => println!("Got IO error: {}", io_error),
    Err(original) => println!("Not an IO error: {}", original),
}
Source

pub fn downcast_ref<E>(&self) -> Option<&E>
where E: Error + Send + Sync + 'static,

Attempts to downcast the inner error to a reference of the concrete type.

Returns Some(&E) if the downcast succeeds, or None if it fails.

§Examples
use http_kit::Error;
use http::StatusCode;
use std::io;

let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let http_err = Error::new(io_err, StatusCode::NOT_FOUND);

if let Some(io_error) = http_err.downcast_ref::<io::Error>() {
    println!("IO error kind: {:?}", io_error.kind());
}
Source

pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
where E: Error + Send + Sync + 'static,

Attempts to downcast the inner error to a mutable reference of the concrete type.

Returns Some(&mut E) if the downcast succeeds, or None if it fails.

§Examples
use http_kit::Error;
use http::StatusCode;
use std::io;

let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let mut http_err = Error::new(io_err, StatusCode::NOT_FOUND);

if let Some(io_error) = http_err.downcast_mut::<io::Error>() {
    // Modify the IO error if needed
}
Source

pub fn into_inner(self) -> Box<dyn Error + Send + Sync + 'static>

Consumes this error and returns the inner error, discarding the status code.

§Examples
use http_kit::Error;
use http::StatusCode;

let err = Error::msg("some error").set_status(StatusCode::BAD_REQUEST);
let inner = err.into_inner();

Trait Implementations§

Source§

impl AsMut<dyn Error + Send> for Error

Source§

fn as_mut(&mut self) -> &mut (dyn Error + Send + 'static)

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<dyn Error + Send> for Error

Source§

fn as_ref(&self) -> &(dyn Error + Send + 'static)

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Error

Source§

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

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

impl Deref for Error

Source§

type Target = dyn Error + Send

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Error

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl Display for Error

Source§

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

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

impl<E: Into<Error>> From<E> for Error

Source§

fn from(error: E) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Box<dyn Error>

Source§

fn from(error: Error) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnwindSafe for Error

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.