• Rene Nochebuena released this 2026-03-25 16:58:33 -06:00 | 0 commits to main since this release

    v0.10.0

    code.nochebuena.dev/go/httputil

    Overview

    httputil removes HTTP boilerplate from business logic. Generic adapter functions wrap
    pure Go functions into http.HandlerFunc values, handling JSON decode, struct validation,
    JSON encode, and error-to-HTTP-status mapping automatically. A single Error helper
    translates any *xerrors.Err to the correct HTTP status and JSON body.

    What's New in v0.10.0

    platformCode in error responses

    Error(w, err) now includes "platformCode" in the JSON body when the *xerrors.Err
    carries one (via WithPlatformCode). Omitted when not set.

    {
      "code": "NOT_FOUND",
      "platformCode": "EMPLOYEE_NOT_FOUND",
      "message": "employee not found"
    }
    

    This pairs with xerrors v0.10.0 which introduced WithPlatformCode. The transport
    layer (httputil) passes it through transparently — no mapping or logic added here.

    Backwards-compatible: error responses for errors without a platform code are
    identical to v0.9.0.

    What's Included

    Handler adapters (generic):

    • Handle[Req, Res any](v Validator, fn func(ctx, Req) (Res, error)) http.HandlerFunc
    • HandleNoBody[Res any](fn func(ctx) (Res, error)) http.HandlerFunc
    • HandleEmpty[Req any](v Validator, fn func(ctx, Req) error) http.HandlerFunc

    Manual handler type:

    • HandlerFunc func(w, r) error — implements http.Handler

    Response helpers:

    • JSON(w, status, v) — encodes v as JSON, sets Content-Type: application/json
    • NoContent(w) — writes 204 No Content
    • Error(w, err) — maps *xerrors.Err to HTTP status; includes platformCode when present (updated)

    xerrors.Code → HTTP status mapping (12 codes):

    Code Status
    ErrInvalidInput 400 Bad Request
    ErrUnauthorized 401 Unauthorized
    ErrPermissionDenied 403 Forbidden
    ErrNotFound 404 Not Found
    ErrAlreadyExists 409 Conflict
    ErrGone 410 Gone
    ErrPreconditionFailed 412 Precondition Failed
    ErrRateLimited 429 Too Many Requests
    ErrInternal 500 Internal Server Error
    ErrNotImplemented 501 Not Implemented
    ErrUnavailable 503 Service Unavailable
    ErrDeadlineExceeded 504 Gateway Timeout

    Installation

    go get code.nochebuena.dev/go/httputil@v0.10.0
    

    Changelog

    See CHANGELOG.md.

    Downloads