api

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package api reads and answers HTTP requests: JSON bodies within a limit, and errors with the status a system's error carries. A system declares each error a person can be shown with Invalid, Forbidden, NotFound, Conflict, or Unavailable, whose text is the sentence the person reads. WriteError sends that sentence with the matching status, and any other error as a logged 500 with a generic message, so no internal detail reaches a browser.

Index

Constants

View Source
const DefaultBodyLimit = 1 << 20

DefaultBodyLimit is the request body size LimitBodies allows on a route with no limit of its own: plenty for a JSON body.

Variables

View Source
var ErrNotJSON = Invalid("The request body is not valid JSON.")

ErrNotJSON is the answer when a request body cannot be decoded.

Functions

func Conflict

func Conflict(text string) error

Conflict is an error for a change the current state refuses, answered with 409.

func Forbidden

func Forbidden(text string) error

Forbidden is an error for an action the person may not take, answered with 403.

func Invalid

func Invalid(text string) error

Invalid is an error for input the system refuses, answered with 400.

func LimitBodies added in v0.8.0

func LimitBodies(limit int64, next http.Handler) http.Handler

LimitBodies caps every request body at limit bytes, so no handler can be made to read without bound. Wrap the mux once. A handler that reads past the limit gets an *http.MaxBytesError, which WriteError answers with 413.

func LimitBody added in v0.8.0

func LimitBody(limit int64, next http.Handler) http.Handler

LimitBody gives one route its own limit, larger or smaller than LimitBodies' default, such as an upload. It may sit anywhere on the route's line before anything that reads the body: it caps the body LimitBodies received, not the one LimitBodies already capped. When two sit on one line, the one nearest the handler wins.

func NotFound

func NotFound(text string) error

NotFound is an error for a record that does not exist, answered with 404.

func ReadJSON added in v0.8.0

func ReadJSON(request *http.Request, value any) error

ReadJSON decodes the request body into value: one JSON value and nothing after it. A body over its limit returns the *http.MaxBytesError, which WriteError answers with 413; anything else that fails returns ErrNotJSON.

func Unavailable added in v0.9.0

func Unavailable(text string) error

Unavailable is an error for a service the system depends on that did not answer, so the person should try again later, answered with 503.

func WriteError

func WriteError(writer http.ResponseWriter, request *http.Request, err error)

WriteError answers with {"error": text}. An error made by Invalid, Forbidden, NotFound, Conflict, or Unavailable, even when wrapped, is sent with its own text and status; an Unavailable one is also logged as a warning with everything it wraps, which says what did not answer. A body over its limit is 413. Any other error is logged with the request and answered with 500 and a generic message.

func WriteJSON

func WriteJSON(writer http.ResponseWriter, status int, value any)

WriteJSON answers with status and value encoded as JSON.

Types

This section is empty.

Jump to

Keyboard shortcuts

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