httputil depends on xerrors (Tier 0) and valid (Tier 1), placing it at Tier 2. No infrastructure or lifecycle dependencies exist in this module.
2.7 KiB
2.7 KiB
Changelog
All notable changes to this module will be documented in this file.
The format is based on Keep a Changelog, and this module adheres to Semantic Versioning.
0.9.0 - 2026-03-18
Added
Handle[Req, Res any](v valid.Validator, fn func(ctx context.Context, req Req) (Res, error)) http.HandlerFunc— decodes the JSON request body intoReq, validates it with the injectedValidator, callsfn, and encodes the result as JSON with HTTP 200; invalid JSON returns 400HandleNoBody[Res any](fn func(ctx context.Context) (Res, error)) http.HandlerFunc— no body decode or validation; callsfnand encodes the result as JSON with HTTP 200; intended for GET and DELETE endpointsHandleEmpty[Req any](v valid.Validator, fn func(ctx context.Context, req Req) error) http.HandlerFunc— decodes and validates the JSON request body, callsfn, returns HTTP 204 on success; intended for write endpoints with no response bodyHandlerFunctype —func(w http.ResponseWriter, r *http.Request) error; implementshttp.Handler; on non-nil return, routes the error throughError(w, err)for automatic status mappingJSON(w http.ResponseWriter, status int, v any)— encodesvas JSON and writes it with the given status code; setsContent-Type: application/jsonNoContent(w http.ResponseWriter)— writes HTTP 204 No ContentError(w http.ResponseWriter, err error)— maps*xerrors.Errcodes to HTTP status codes and writes a{"code": "...", "message": "..."}JSON body; includes any extra fields from the error; falls back to 500 for unknown errorsxerrors.Codeto HTTP status mapping (12 codes):ErrInvalidInput→ 400,ErrUnauthorized→ 401,ErrPermissionDenied→ 403,ErrNotFound→ 404,ErrAlreadyExists→ 409,ErrGone→ 410,ErrPreconditionFailed→ 412,ErrRateLimited→ 429,ErrInternal→ 500,ErrNotImplemented→ 501,ErrUnavailable→ 503,ErrDeadlineExceeded→ 504
Design Notes
- Business functions wrapped by
Handle,HandleNoBody, andHandleEmptyhave nohttp.ResponseWriteror*http.Requestin their signatures, making them callable directly in unit tests withcontext.Background()and a typed request value. Erroris the single translation point fromxerrors.Codeto HTTP status; all handler adapters route through it, preventing fragmented status code contracts across the codebase.- Validation is injected via
valid.Validatorand runs before the business function is called; an invalid request never reaches business logic.