pub struct HttpError { /* private fields */ }Expand description
HTTP error response with stable client-facing JSON shape.
HttpError constructors produce the legacy/simple body
{ "error": { "code": "...", "message": "..." } }. When
ErrorEnvelopeLayer is installed, that body is wrapped into the
production envelope with status, timestamp, path, and request ID fields.
Client-error constructors such as Self::bad_request and
Self::not_found expose the message you provide. Use
Self::internal_server_error for 500 responses that must not leak
implementation details; the production envelope also masks any 5xx response
message to "internal server error" and clears details.
Implementations§
Source§impl HttpError
impl HttpError
Sourcepub fn new(
status: StatusCode,
code: &'static str,
message: impl Into<String>,
) -> Self
pub fn new( status: StatusCode, code: &'static str, message: impl Into<String>, ) -> Self
Creates an HTTP error with an explicit status, code, and message.
For non-5xx statuses, the message is client-facing. For 5xx statuses,
prefer Self::internal_server_error unless the response is guaranteed
to be safe; ErrorEnvelopeLayer will still mask 5xx messages.
Sourcepub fn bad_request(message: impl Into<String>) -> Self
pub fn bad_request(message: impl Into<String>) -> Self
Creates a 400 bad request error with a client-facing message.
Creates a 401 unauthorized error with a client-facing message.
Sourcepub fn forbidden(message: impl Into<String>) -> Self
pub fn forbidden(message: impl Into<String>) -> Self
Creates a 403 forbidden error with a client-facing message.
Sourcepub fn not_found(message: impl Into<String>) -> Self
pub fn not_found(message: impl Into<String>) -> Self
Creates a 404 not found error with a client-facing message.
Sourcepub fn conflict(message: impl Into<String>) -> Self
pub fn conflict(message: impl Into<String>) -> Self
Creates a 409 conflict error with a client-facing message.
Sourcepub fn too_many_requests(message: impl Into<String>) -> Self
pub fn too_many_requests(message: impl Into<String>) -> Self
Creates a 429 too many requests error with a client-facing message.
Sourcepub fn unprocessable_entity(message: impl Into<String>) -> Self
pub fn unprocessable_entity(message: impl Into<String>) -> Self
Creates a 422 unprocessable entity error with a client-facing message.
Sourcepub fn internal_server_error() -> Self
pub fn internal_server_error() -> Self
Creates a sanitized 500 internal server error.
The message is always "internal server error" so callers do not
accidentally expose database errors, stack traces, or upstream payloads.
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the HTTP status code.
Trait Implementations§
impl Eq for HttpError
Source§impl Error for HttpError
impl Error for HttpError
1.30.0 · 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
use the Display impl or to_string()
Source§impl IntoResponse for HttpError
impl IntoResponse for HttpError
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
impl StructuralPartialEq for HttpError
Auto Trait Implementations§
impl Freeze for HttpError
impl RefUnwindSafe for HttpError
impl Send for HttpError
impl Sync for HttpError
impl Unpin for HttpError
impl UnsafeUnpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
tower::Layer to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Service by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Service and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
MakeService and no state. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
MakeService which stores information
about the incoming connection and has no state. Read more