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
impl Error
Sourcepub fn new<E, S>(error: E, status: S) -> Self
pub fn new<E, S>(error: E, status: S) -> Self
Creates a new Error
from any error type with the given HTTP status code.
§Arguments
error
- Any error type that can be converted toanyhow::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);
Sourcepub fn msg<S>(msg: S) -> Self
pub fn msg<S>(msg: S) -> Self
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 implementsDisplay + 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));
Sourcepub fn set_status<S>(self, status: S) -> Self
pub fn set_status<S>(self, status: S) -> Self
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);
Sourcepub fn status(&self) -> StatusCode
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);
Sourcepub fn downcast<E>(self) -> Result<Box<E>, Self>
pub fn downcast<E>(self) -> Result<Box<E>, Self>
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),
}
Sourcepub fn downcast_ref<E>(&self) -> Option<&E>
pub fn downcast_ref<E>(&self) -> Option<&E>
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());
}
Sourcepub fn downcast_mut<E>(&mut self) -> Option<&mut E>
pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
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
}
Sourcepub fn into_inner(self) -> Box<dyn Error + Send + Sync + 'static>
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();