pub struct Error { /* private fields */ }
Expand description
The error type used by the Hyperdrive library.
This type can be turned into an HTTP response and sent back to a client.
Implementations§
Source§impl Error
impl Error
Sourcepub fn from_status(status: StatusCode) -> Self
pub fn from_status(status: StatusCode) -> Self
Creates an error that contains just the given StatusCode
.
§Panics
This will panic when called with a status
that does not indicate a
client or server error.
Sourcepub fn with_source<S>(status: StatusCode, source: S) -> Selfwhere
S: Into<BoxedError>,
pub fn with_source<S>(status: StatusCode, source: S) -> Selfwhere
S: Into<BoxedError>,
Creates an error from an HTTP error code and an underlying error that caused this one.
Responding with the returned Error
will not send a response body back
to the client.
§Parameters
status
: The HTTPStatusCode
describing the error.source
: The underlying error that caused this one. Any type implementingstd::error::Error + Send + Sync
can be passed here.
§Panics
This will panic when called with a status
that does not indicate a
client or server error.
Sourcepub fn wrong_method<M>(allowed_methods: M) -> Self
pub fn wrong_method<M>(allowed_methods: M) -> Self
Creates an error with status code 405 Method Not Allowed
and includes
the allowed set of HTTP methods.
This is called by the code generated by #[derive(FromRequest)]
and
usually does not need to be called by the user (it may be difficult to
determine the full set of allowed methods for a given path).
Calling Error::response
on the error created by this function will
automatically include an Allow
header listing all allowed methods.
Including this header is required by RFC 7231.
§Parameters
allowed_methods
: The list of allowed HTTP methods for the path in the request. This can be empty, but usually should contain at least one method.
Sourcepub fn http_status(&self) -> StatusCode
pub fn http_status(&self) -> StatusCode
Returns the HTTP status code that describes this error.
Sourcepub fn response(&self) -> Response<()>
pub fn response(&self) -> Response<()>
Creates an HTTP response for indicating this error to the client.
No body will be provided (hence the ()
body type), but the caller can
map
the result to supply one.
§Example
Call map
on the response to supply your own HTTP payload:
use http::StatusCode;
use hyper::Body;
let error = Error::from_status(StatusCode::NOT_FOUND);
let response = error.response()
.map(|()| Body::from("oh no!"));
Sourcepub fn allowed_methods(&self) -> Option<&[&'static Method]>
pub fn allowed_methods(&self) -> Option<&[&'static Method]>
If self
is a 405 Method Not Allowed
error, returns the list of
allowed methods.
Returns None
if self
is a different kind of error.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
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> 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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more