problem

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 6 Imported by: ? 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 problem implements RFC 9457 "Problem Details for HTTP APIs".

Index

Constants

View Source
const (
	MustNotBeBlank    = "Must not be blank"
	MustNotBeEmpty    = "Must not be empty"
	InvalidValue      = "Invalid value"
	ValidEmail        = "Must be a valid email address"
	ValidURL          = "Must be a valid web address"
	BadFormat         = "Is not in the expected format"
	MustBeMoreThan0   = "Must be more than 0"
	MustNotBeNegative = "Must not be negative"
	MustBeInThePast   = "Must be in the past"
	MustBeInTheFuture = "Must be in the future"
	MustNotBeInFuture = "Must not be in the future"
	MustNotBeInPast   = "Must not be in the past"
)

The words roux's forms show for the common rules, so every endpoint speaks to the person filling in a form the same way, and in the browser's words (contracts/messages.json is the list both sides test against).

View Source
const ContentType = "application/problem+json"

ContentType is the media type for problem details responses.

Variables

This section is empty.

Functions

func MustBeAfter

func MustBeAfter(other string) string

MustBeAfter is the message for a value not after another, named as the form labels it: "Must be after starts at".

func MustBeAtLeast

func MustBeAtLeast(n int64) string

MustBeAtLeast is the message for a number under its minimum: "Must be at least 5".

func MustBeAtMost

func MustBeAtMost(n int64) string

MustBeAtMost is the message for a number over its maximum: "Must be at most 5".

func MustBeBetween

func MustBeBetween(least, most int) string

MustBeBetween is the message for a length out of range: "Must be between 1 and 80 characters".

func MustBeOneOf

func MustBeOneOf(values ...string) string

MustBeOneOf is the message for a value outside a fixed set: "Must be one of A, B".

func Write

func Write(w http.ResponseWriter, r *http.Request, p *Problem)

Write sends p as a problem+json response. The instance defaults to the request path, and traceId is filled from the active span (if any).

Types

type Problem

type Problem struct {
	Type     string      `json:"type,omitempty"`
	Title    string      `json:"title,omitempty"`
	Status   int         `json:"status,omitempty"`
	Detail   string      `json:"detail,omitempty"`
	Instance string      `json:"instance,omitempty"`
	Errors   []Violation `json:"errors,omitempty"`  // extension member
	TraceID  string      `json:"traceId,omitempty"` // extension member: correlates with logs/traces
	Code     string      `json:"code,omitempty"`    // extension member: a stable machine-readable error code
	ErrorID  string      `json:"errorId,omitempty"` // extension member on a 500: matches the response to the log entry

	// Headers are sent with the response (WWW-Authenticate on a 401, say); never in the body.
	Headers http.Header `json:"-"`
}

Problem is an RFC 9457 problem details object. It also implements error, so handlers can simply return one.

func BadRequest

func BadRequest(detail string) *Problem

func Conflict

func Conflict(detail string) *Problem

func Forbidden

func Forbidden() *Problem

func Internal

func Internal() *Problem

func InvalidField

func InvalidField(field, message string) *Problem

InvalidField is a Validation with one field's problem.

func MethodNotAllowed

func MethodNotAllowed(detail string) *Problem

func New

func New(status int, detail string) *Problem

New creates a problem for the given status with a standard title.

func NotFound

func NotFound(detail string) *Problem

func StaleVersion

func StaleVersion() *Problem

StaleVersion is the 409 for an edit made from a version that is no longer current.

func Unauthorized

func Unauthorized() *Problem

func Validation

func Validation(violations ...Violation) *Problem

Validation returns the 400 roux answers for invalid input: the fields that are wrong, sorted by field and then message so a response is stable.

func (*Problem) Error

func (p *Problem) Error() string

func (*Problem) WithHeader

func (p *Problem) WithHeader(key, value string) *Problem

WithHeader adds a response header to the problem and returns it.

func (*Problem) WriteHeaders

func (p *Problem) WriteHeaders(w http.ResponseWriter)

WriteHeaders sends the problem's own headers; a renderer that writes its own body calls it.

type Violation

type Violation struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

Violation describes a single invalid field in a request.

Jump to

Keyboard shortcuts

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