Help us shape the future of Go! Take the 2026 Go Developer Survey

problem

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

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

ContentType is the standard RFC 9457 media type for Problem Details JSON payloads.

Variables

This section is empty.

Functions

func Write added in v0.2.0

func Write(w http.ResponseWriter, p Problem)

Write sets the Content-Type header to "application/problem+json", writes the status code, and streams the serialized Problem payload to w.

Types

type InvalidParam

type InvalidParam struct {
	Name   string `json:"name"`
	Reason string `json:"reason"`
}

InvalidParam describes a single schema constraint violation for 422 Unprocessable Entity responses.

type Problem

type Problem struct {
	Type          string         `json:"type,omitempty"`
	Title         string         `json:"title"`
	Status        int            `json:"status"`
	Detail        string         `json:"detail,omitempty"`
	Instance      string         `json:"instance,omitempty"`
	InvalidParams []InvalidParam `json:"invalid_params,omitempty"`
	Extensions    map[string]any `json:"-"`
}

Problem represents an RFC 9457 compliant HTTP error payload.

It implements the standard Go [error] interface. Custom metadata added to Extensions is flattened directly into the root JSON object during marshaling. Standard RFC 9457 attributes take precedence over extension keys sharing identical names.

func New

func New(status int, detail ...string) Problem

New constructs a Problem with the supplied HTTP status code and optional detail explanation.

If status is not a recognized HTTP status code, Title defaults to "Error". Type is automatically generated as an RFC 9457 URN based on Title (e.g. "urn:hclapi:error:not-found").

func Validation added in v0.2.1

func Validation(detail string, params ...InvalidParam) Problem

Validation constructs an RFC 9457 HTTP 422 Unprocessable Entity problem pre-configured with schema constraint violation parameters.

func (Problem) Error

func (p Problem) Error() string

Error implements [error].

func (Problem) MarshalJSON

func (p Problem) MarshalJSON() ([]byte, error)

MarshalJSON flattens Extensions directly into the root JSON object while enforcing RFC 9457 attribute types and automatic defaults for Title and Type if omitted.

func (*Problem) UnmarshalJSON added in v0.2.1

func (p *Problem) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals RFC 9457 JSON, extracting known members into typed struct fields and collecting all unrecognized keys into the Extensions map.

func (Problem) WithExtension added in v0.2.1

func (p Problem) WithExtension(key string, value any) Problem

WithExtension sets a key-value pair in Extensions, allocating the underlying map if nil.

func (Problem) WithInstance added in v0.2.1

func (p Problem) WithInstance(instance string) Problem

WithInstance returns a copy of p with the Instance attribute set.

Jump to

Keyboard shortcuts

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