response

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package response constructs typed Huma outputs and safe HTTP problem details. Huma owns serialization, headers and OpenAPI; files and streams use native outputs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(ctx context.Context, err error) error

Error translates a service failure into safe Huma problem details. Call it when returning a service error from an HTTP handler. Nil stays nil; native Huma status errors and headers are preserved. No global Huma hooks are changed.

func LogError

func LogError(ctx context.Context, err error)

LogError records a safe operational failure, including errors after a stream starts. It never writes a response; a broken stream must abort its transport.

func RequestID

func RequestID(ctx context.Context) string

RequestID returns the server-generated correlation ID for this request. It returns an empty string when no ID has been attached to the context.

func Transform

func Transform(_ huma.Context, _ string, value any) (any, error)

Transform integrates safe problem details with Huma without changing its global hooks. Huma owns validation; only redact transport diagnostics here: no submitted values or parser internals leave the HTTP boundary, including missing fields.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID attaches the server-generated correlation ID to a request context. The API server calls this once before middleware and handlers run.

func WriteError

func WriteError(ctx huma.Context, err error)

WriteError rejects a request from Huma middleware. Return without invoking next.

func WriteHTTPError

func WriteHTTPError(ctx context.Context, w http.ResponseWriter, err error)

WriteHTTPError writes a service failure from a standard net/http handler. Call before committing headers or body bytes, then return from the handler.

Types

type Empty

type Empty struct{}

Empty declares no response body. Huma defaults bodyless outputs to status 204.

func NoContent

func NoContent() *Empty

type Envelope

type Envelope[T any] struct {
	Data T              `json:"data"`
	Meta map[string]any `json:"meta,omitempty" doc:"Optional operation-specific metadata"`
}

Envelope keeps the result's concrete type available to Huma's schema generator. Data is always present, including null for a nil result.

type JSON

type JSON[T any] struct {
	Body   Envelope[T]
	Status int
}

JSON is a native Huma output. Construct it with NewJSON or a status shortcut; Huma uses Status directly, so a literal with an unset Status is invalid.

func Accepted

func Accepted[T any](data T) *JSON[T]

func Created

func Created[T any](data T) *JSON[T]

func NewJSON

func NewJSON[T any](data T, status int) *JSON[T]

NewJSON constructs a successful response whose status permits a JSON body. Invalid status values are programmer errors. Declare the same success status on the Huma operation so its OpenAPI contract matches the runtime response.

func OK

func OK[T any](data T) *JSON[T]

func (*JSON[T]) WithMeta

func (r *JSON[T]) WithMeta(meta map[string]any) *JSON[T]

WithMeta sets optional JSON object metadata. It copies the map; nested values remain caller-owned and must not be mutated while the response is being sent.

type Problem

type Problem struct {
	huma.ErrorModel
	Meta map[string]any `json:"meta,omitempty" doc:"Optional operation-specific metadata"`
}

Problem retains Huma's RFC 9457 fields and exhaustive errors list, with optional operation-specific metadata. There is no surrounding envelope.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL