Skip to main content

Module response

Module response 

Source
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§

ErrorResult
Handler failure value: message, optional JSON payload, and HTTP status code. Returned as Err from handlers and rendered as an {"status":"error",...} envelope.
GenericTypedResult
Generic JSON-compatible result: serializes its data value directly, or "null" when absent. Useful for endpoints whose payload is already a serde_json::Value.
ServiceResult
Standard success envelope: serializes as {"status","message","data"} unless ServiceResult::stripped is used, in which case only data is serialized. Type parameter T is the data payload. code and response_type control the HTTP status and content type but are not serialized into the body.

Enums§

ResponseType
Content type used when rendering a result: selects the Content-Type header, and File additionally sets Content-Disposition: attachment.

Traits§

TypedServiceResult
Renderable handler result: message, HTTP status, content type, and body serializer. Implemented by ServiceResult, GenericTypedResult, and ErrorResult, so handlers can return them as Box<dyn TypedServiceResult> for mixed payload shapes.

Functions§

build_response
Renders any TypedServiceResult as an http::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 an http::Response<String>; see build_response.
error_response
Renders an ErrorResult as an http::Response<String> with an "error" envelope.