pub enum ApiResponse<T: Serialize> {
Success(StatusCode, T),
Error(StatusCode, String),
}Expand description
A unified response type that handlers can return.
Converts into a properly-typed axum Response with the correct status code
and a JSON body using either [SuccessBody] or [ErrorBody].
§Usage
ⓘ
async fn get_user() -> ApiResponse<User> {
let user = User { name: "Alice".into() };
ApiResponse::ok(user)
}
async fn not_found() -> ApiResponse<()> {
ApiResponse::error(StatusCode::NOT_FOUND, "resource not found")
}Variants§
Success(StatusCode, T)
Error(StatusCode, String)
Implementations§
Source§impl<T: Serialize> ApiResponse<T>
impl<T: Serialize> ApiResponse<T>
Sourcepub fn success(status: StatusCode, data: T) -> Self
pub fn success(status: StatusCode, data: T) -> Self
Arbitrary success status with a data payload.
Sourcepub fn error(status: StatusCode, message: impl Into<String>) -> Self
pub fn error(status: StatusCode, message: impl Into<String>) -> Self
Error response with a status code and message.
Sourcepub fn bad_request(message: impl Into<String>) -> Self
pub fn bad_request(message: impl Into<String>) -> Self
400 Bad Request.
401 Unauthorized.
Sourcepub fn internal_error(message: impl Into<String>) -> Self
pub fn internal_error(message: impl Into<String>) -> Self
500 Internal Server Error.
Trait Implementations§
Source§impl<T: Serialize> IntoResponse for ApiResponse<T>
impl<T: Serialize> IntoResponse for ApiResponse<T>
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
Auto Trait Implementations§
impl<T> Freeze for ApiResponse<T>where
T: Freeze,
impl<T> RefUnwindSafe for ApiResponse<T>where
T: RefUnwindSafe,
impl<T> Send for ApiResponse<T>where
T: Send,
impl<T> Sync for ApiResponse<T>where
T: Sync,
impl<T> Unpin for ApiResponse<T>where
T: Unpin,
impl<T> UnsafeUnpin for ApiResponse<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for ApiResponse<T>where
T: UnwindSafe,
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
Mutably borrows from an owned value. Read more