httpjson

package
v0.1.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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"
	CodeUnavailable            = "unavailable"
	// 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.

View Source
const MaxBodyBytes = 1 << 20

MaxBodyBytes limits JSON request bodies.

Variables

This section is empty.

Functions

func Decode

func Decode(w http.ResponseWriter, r *http.Request, v any) bool

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

func Status(err error) (status int, code string)

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

func WriteErr(w http.ResponseWriter, r *http.Request, logger *slog.Logger, err error)

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

type ErrorDetail struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorDetail describes an API error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL