pub struct ApiErrorBody<E = Value> {
pub message: String,
pub status: u16,
pub code: String,
pub error: E,
}Expand description
Envelope for HTTP error responses produced by #[derive(ApiError)].
The type parameter E determines the shape of the error field.
When generated by the macro, E is the concrete error enum — giving
frontends a fully typed payload. The default (serde_json::Value)
is used by layer contributions and hand-rolled error responses where
the concrete type is not known.
§Example
use doxa::ApiErrorBody;
let body: ApiErrorBody<()> =
ApiErrorBody::new(404, "not_found", "widget 7 does not exist", ());
let json = serde_json::to_value(&body).unwrap();
assert_eq!(json["status"], 404);
assert_eq!(json["code"], "not_found");
assert_eq!(json["message"], "widget 7 does not exist");Fields§
§message: StringHuman-readable message from the source error’s Display impl.
status: u16HTTP status code, mirrored from the response status line.
code: StringApplication-level error code suitable for client-side
discrimination (e.g. "not_found", "validation_error").
error: ETyped, machine-readable error data. When E is a concrete error
enum, this field carries the enum’s serde serialization. When E
is the default serde_json::Value, it accepts any JSON value.
Implementations§
Source§impl<E: Serialize> ApiErrorBody<E>
impl<E: Serialize> ApiErrorBody<E>
Sourcepub fn new(
status: u16,
code: impl Into<String>,
message: impl Into<String>,
error: E,
) -> Self
pub fn new( status: u16, code: impl Into<String>, message: impl Into<String>, error: E, ) -> Self
Construct an ApiErrorBody from its raw fields.
Most callers should let #[derive(ApiError)] build this for
them; this constructor exists for hand-rolled error responses,
layer contributions, and tests.
Trait Implementations§
Source§impl<E: Clone> Clone for ApiErrorBody<E>
impl<E: Clone> Clone for ApiErrorBody<E>
Source§fn clone(&self) -> ApiErrorBody<E>
fn clone(&self) -> ApiErrorBody<E>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more