- Error(w, err) now extracts PlatformCode() from *xerrors.Err and includes "platformCode" in the JSON body when set; omitted otherwise - errorBody updated to accept platformCode as an explicit parameter - Upgraded code.nochebuena.dev/go/xerrors dependency to v0.10.0 - Added two new tests: platformCode included when set, omitted when absent Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Reviewed-on: #1 Reviewed-by: Rene Nochebuena <rene@noreply.nochebuena.dev> Co-authored-by: Claude Code <claude@nochebuena.dev> Co-committed-by: Claude Code <claude@nochebuena.dev>
3.3 KiB
3.3 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.10.0 - 2026-03-25
Changed
Error(w http.ResponseWriter, err error)— now includes"platformCode"in the JSON error body when the*xerrors.Errcarries one; omitted otherwise- Upgraded
code.nochebuena.dev/go/xerrorsdependency tov0.10.0
Design Notes
platformCodeis the domain-layer error identifier introduced inxerrorsv0.10.0. It travels throughErrortransparently — no transport-layer logic depends on it.- Backwards-compatible: error responses without a platform code are identical to v0.9.0.
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.