httperr

package
v1.37.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package httperr provides RFC 9457 Problem Details for HTTP API error responses.

RFC 9457 defines a standard JSON format for conveying machine-readable error details in HTTP responses. This package implements a minimal helper for producing "application/problem+json" responses with the required fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WantsProblemJSON added in v1.37.0

func WantsProblemJSON(h http.Header) bool

WantsProblemJSON reports whether the Accept header of a request names application/problem+json with a q value above 0 and not above 1. The wildcards */* and application/* do not count, and the q values of the ranges are not compared: a client that names the type can parse it. A range that does not parse is skipped.

func WriteMetaError added in v1.37.0

func WriteMetaError(w http.ResponseWriter, in MetaErrorInput)

WriteMetaError writes a PeeringDB /api/ error body as application/json: {"meta": {"error": "<text>", ...}}, plus "data": [] when in.EmptyData is set.

func WriteProblem

func WriteProblem(w http.ResponseWriter, input WriteProblemInput)

WriteProblem writes an RFC 9457 problem detail JSON response to w. It sets the Content-Type to "application/problem+json", writes the HTTP status code, and encodes the problem detail as JSON.

Types

type MetaErrorInput added in v1.37.0

type MetaErrorInput struct {
	// Status is the HTTP status code.
	Status int
	// Error is the meta.error text.
	Error string
	// Meta holds more meta keys. An "error" key here is ignored.
	Meta map[string]any
	// EmptyData adds "data": [] (the upstream unique-query 404,
	// rest.py:809-815).
	EmptyData bool
}

MetaErrorInput holds the parameters of a PeeringDB /api/ error body. Upstream 2.83.0 renderers.py:134-148 writes a 4xx body as {"meta": {"error": "<detail>"}} with no "data" key.

type ProblemDetail

type ProblemDetail struct {
	Type     string `json:"type"`
	Title    string `json:"title"`
	Status   int    `json:"status"`
	Detail   string `json:"detail,omitempty"`
	Instance string `json:"instance,omitempty"`
}

ProblemDetail represents an RFC 9457 Problem Details response. The Type field defaults to "about:blank" when no specific problem type URI applies. Title and Status are always present; Detail and Instance are optional and omitted from the JSON when empty.

func NewProblemDetail

func NewProblemDetail(input WriteProblemInput) ProblemDetail

NewProblemDetail constructs a ProblemDetail struct from the given input without writing an HTTP response. This is useful for embedding problem details inside other JSON response structures (e.g., terminal JSON mode).

type WriteProblemInput

type WriteProblemInput struct {
	Status   int
	Title    string
	Detail   string
	Instance string
}

WriteProblemInput holds parameters for writing a problem detail response. Status is required. Title defaults to the standard HTTP status text when empty. Detail and Instance are optional.

Jump to

Keyboard shortcuts

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