Rene Nochebuena b794bf668e fix(httputil): rename error body field platformCode → platform_code (#2)
errorBody() map key corrected from "platformCode" to "platform_code" to
match the OpenAPI spec snake_case convention. All HTTP error responses
produced by Error(w, err) now emit "platform_code" in the JSON body.
PR.md added to .gitignore.
Depends on xerrors v1.0.2 (go.mod bumped in follow-up commit after xerrors tag).

Reviewed-on: #2
Co-authored-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
Co-committed-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
2026-05-21 19:34:41 -06:00

httputil

Typed HTTP handler adapters and response helpers for stdlib net/http.

Install

go get code.nochebuena.dev/go/httputil

Typed handlers

// JSON body + validation + typed response
r.Post("/orders", httputil.Handle(validator, svc.CreateOrder))

// No request body (GET / DELETE)
r.Get("/orders/{id}", httputil.HandleNoBody(svc.GetOrder))

// Request body, no response body (204)
r.Delete("/orders/{id}", httputil.HandleEmpty(validator, svc.DeleteOrder))

// Manual handler with centralised error mapping
r.Get("/raw", httputil.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
    data, err := svc.Load(r.Context(), chi.URLParam(r, "id"))
    if err != nil {
        return err
    }
    return httputil.JSON(w, http.StatusOK, data)
}))

Error mapping

xerrors.Code → HTTP status:

Code Status
ErrInvalidInput 400
ErrUnauthorized 401
ErrPermissionDenied 403
ErrNotFound 404
ErrAlreadyExists 409
ErrInternal 500
ErrNotImplemented 501
ErrUnavailable 503
ErrDeadlineExceeded 504
unknown 500

Error response body:

{"code": "NOT_FOUND", "message": "record not found"}

Fields from xerrors.Err are merged into the top-level response object.

Dependencies

  • code.nochebuena.dev/go/xerrors
  • code.nochebuena.dev/go/valid
Description
Typed handler adapters and JSON response helpers.
Readme MIT 67 KiB
2026-05-21 19:35:23 -06:00
Languages
Go 100%