pub struct ErrorResponseContext { /* private fields */ }
Expand description
A structured context containing error information for generating HTTP responses.
This struct holds the essential components of an error response:
- HTTP status code
- Error code (for API consumers)
- Human-readable error message
It can be used by custom error response functions to access error details and generate appropriate responses in any desired format.
§Example
use axum_error_handler::ErrorResponseContext;
let context = ErrorResponseContext::builder()
.status_code(404)
.code("NOT_FOUND".to_string())
.message("Resource not found".to_string())
.build();
assert_eq!(context.status_code(), Some(404));
assert_eq!(context.code(), Some(&"NOT_FOUND".to_string()));
Implementations§
Source§impl ErrorResponseContext
impl ErrorResponseContext
Sourcepub fn builder() -> ErrorResponseBuilder
pub fn builder() -> ErrorResponseBuilder
Creates a new builder for constructing an ErrorResponseContext
.
This is the recommended way to create a new context with specific values.
§Example
use axum_error_handler::ErrorResponseContext;
let context = ErrorResponseContext::builder()
.status_code(400)
.code("VALIDATION_ERROR".to_string())
.message("Invalid input provided".to_string())
.build();
Sourcepub fn new() -> ErrorResponseContext
pub fn new() -> ErrorResponseContext
Creates a new empty ErrorResponseContext
.
All fields will be None
initially. Use the builder pattern or
the setter methods to populate the context.
Sourcepub fn status_code(&self) -> Option<u16>
pub fn status_code(&self) -> Option<u16>
Returns the HTTP status code if set.
§Returns
Some(status_code)
if a status code was set, None
otherwise.
Trait Implementations§
Source§impl Clone for ErrorResponseContext
impl Clone for ErrorResponseContext
Source§fn clone(&self) -> ErrorResponseContext
fn clone(&self) -> ErrorResponseContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl IntoResponse for ErrorResponseContext
Default implementation for converting an ErrorResponseContext
into an Axum HTTP response.
impl IntoResponse for ErrorResponseContext
Default implementation for converting an ErrorResponseContext
into an Axum HTTP response.
This implementation creates a standardized JSON response with the following format:
{
"result": null,
"error": {
"code": "ERROR_CODE",
"message": "Error description"
}
}
§Defaults
- Status code: 500 (Internal Server Error) if not specified
- Error code: “UNKNOWN_ERROR” if not specified
- Message: “An error occurred” if not specified
§Example
use axum::response::IntoResponse;
use axum_error_handler::ErrorResponseContext;
let context = ErrorResponseContext::builder()
.status_code(404)
.code("NOT_FOUND".to_string())
.message("The requested resource was not found".to_string())
.build();
let response = context.into_response();
// Creates a 404 response with JSON body
Source§fn into_response(self) -> Response<Body>
fn into_response(self) -> Response<Body>
Auto Trait Implementations§
impl Freeze for ErrorResponseContext
impl RefUnwindSafe for ErrorResponseContext
impl Send for ErrorResponseContext
impl Sync for ErrorResponseContext
impl Unpin for ErrorResponseContext
impl UnwindSafe for ErrorResponseContext
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