Expand description
Typed HTTP responses: success payloads, errors, and content types.
Handlers return Result<ServiceResult<T>, ErrorResult>: ServiceResult is the
standard JSON envelope (status/message/data) plus status code and content type,
and ErrorResult is its fallible counterpart. TypedServiceResult is the
object-safe trait both implement, so handlers needing heterogeneous payloads can
return Box<dyn TypedServiceResult> (see ServiceResult::boxed). Use
build_response / error_response to render them into http::Response<String>
with correlation headers.
ⓘ
async fn get_user(ctx: CorrelationContext) -> Result<ServiceResult<User>, ErrorResult> {
let user = ctx.body::<User>()?;
Ok(ServiceResult::ok("Fetched", user))
}Modules§
- html
- HTML responses: shortcut for returning rendered markup from handlers.
Structs§
- Error
Result - Handler failure value: message, optional JSON payload, and HTTP status code.
Returned as
Errfrom handlers and rendered as an{"status":"error",...}envelope. - Generic
Typed Result - Generic JSON-compatible result: serializes its
datavalue directly, or"null"when absent. Useful for endpoints whose payload is already aserde_json::Value. - Service
Result - Standard success envelope: serializes as
{"status","message","data"}unlessServiceResult::strippedis used, in which case onlydatais serialized. Type parameterTis thedatapayload.codeandresponse_typecontrol the HTTP status and content type but are not serialized into the body.
Enums§
- Response
Type - Content type used when rendering a result: selects the
Content-Typeheader, andFileadditionally setsContent-Disposition: attachment.
Traits§
- Typed
Service Result - Renderable handler result: message, HTTP status, content type, and body serializer.
Implemented by
ServiceResult,GenericTypedResult, andErrorResult, so handlers can return them asBox<dyn TypedServiceResult>for mixed payload shapes.
Functions§
- build_
response - Renders any
TypedServiceResultas anhttp::Response<String>with correlation headers (X-Request-ID,X-Correlation-ID,X-Correlation-Flow) and a content type derived from the result. Serialization failures fall back to the result message as body. - build_
response_ service - Renders a
ServiceResult<T>as anhttp::Response<String>; seebuild_response. - error_
response - Renders an
ErrorResultas anhttp::Response<String>with an"error"envelope.