Documentation
¶
Index ¶
Constants ¶
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 ¶
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 ¶
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) MarshalJSON ¶
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
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
WithExtension sets a key-value pair in Extensions, allocating the underlying map if nil.
func (Problem) WithInstance ¶ added in v0.2.1
WithInstance returns a copy of p with the Instance attribute set.