Documentation
¶
Overview ¶
Package httpjson contains JSON request/response helpers shared by HTTP handlers, including the error envelope from ADR-0006:
{"error": {"code": "not_found", "message": "..."}}
Index ¶
- Constants
- func Decode(w http.ResponseWriter, r *http.Request, v any) bool
- func Status(err error) (status int, code string)
- func Write(w http.ResponseWriter, status int, v any)
- func WriteErr(w http.ResponseWriter, r *http.Request, logger *slog.Logger, err error)
- func WriteError(w http.ResponseWriter, status int, code, message string)
- func WriteRateLimited(w http.ResponseWriter, retryAfter time.Duration)
- type ErrorBody
- type ErrorDetail
Constants ¶
const ( CodeInvalidArgument = "invalid_argument" CodeUnauthenticated = "unauthenticated" CodeForbidden = "forbidden" CodeCSRF = "csrf_failed" CodeNotFound = "not_found" CodeConflict = "conflict" CodeRateLimited = "rate_limited" CodeUnsupportedType = "unsupported_media_type" CodeInternal = "internal" // CodePasswordChangeRequired: the session may only change its temporary password (#74). CodePasswordChangeRequired = "password_change_required" // CodeNerdctlIncompatible: the host nerdctl is missing or its version does not match // (ADR-0011); nerdui is read-only. CodeNerdctlIncompatible = "nerdctl_incompatible" )
Error codes (ADR-0006). Keep in sync with api/openapi.yaml.
const MaxBodyBytes = 1 << 20
MaxBodyBytes limits JSON request bodies.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode reads a JSON body into v. It requires Content-Type application/json (which also prevents cross-site form posts), limits the body size and rejects unknown fields. On failure it writes the error response and returns false.
func Status ¶
Status maps an error from containerd/nerdctl (errdefs) to an HTTP status and error code (ADR-0006). Unknown errors are internal.
func Write ¶
func Write(w http.ResponseWriter, status int, v any)
Write writes v as JSON with the given status.
func WriteErr ¶
WriteErr writes err using Status. Client-side errors carry their message; internal errors are logged and answered with a generic message so details do not leak.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, code, message string)
WriteError writes the error envelope.
func WriteRateLimited ¶
func WriteRateLimited(w http.ResponseWriter, retryAfter time.Duration)
WriteRateLimited writes 429 with a Retry-After header.
Types ¶
type ErrorBody ¶
type ErrorBody struct {
Error ErrorDetail `json:"error"`
}
ErrorBody is the error envelope.
type ErrorDetail ¶
ErrorDetail describes an API error.