Error Handling¶
Standardize errors so clients can rely on status codes and payload shape.
Basic patterns¶
use spikard::{App, get, RequestContext, Response};
use axum::http::StatusCode;
use serde_json::json;
let mut app = App::new();
app.route(
get("/fail"),
|_ctx: RequestContext| async {
let response = Response {
status_code: 400,
content: Some(json!({"error": "bad"})),
headers: Default::default(),
};
Ok(axum::http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(axum::body::Body::from(response.content.unwrap().to_string()))?)
},
)?;
Tips¶
- Prefer structured bodies (RFC 9457 style) with
type,title,detail,statusfields. - Propagate request IDs in errors for tracing.
- Short-circuit auth/validation failures in middleware when possible.