problem

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package problem is the one error shape the API returns: RFC 9457 problem details (the revision of RFC 7807), served as application/problem+json.

A Problem is an error, so a handler returns one; the huma hook in huma.go turns every framework error into the same shape, which is why there is no second error type anywhere in the kernel.

Derived from github.com/septagon-oss/pk-problem (Apache-2.0); see NOTICE.

Index

Constants

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

ContentType is the media type of a problem response.

Variables

This section is empty.

Functions

func HumaError

func HumaError(status int, message string, errs ...error) huma.StatusError

HumaError is the hook that makes huma speak problem details. Assign it once, where the API is built:

huma.NewError = problem.HumaError

It is a plain function rather than an init() so that the wiring is visible at the call site, like every other wire in this repository.

A 5xx never carries its message to the client: at that point the message is as likely to be a driver string as a sentence. The cause stays reachable through errors.Unwrap for the logger.

Types

type Problem

type Problem struct {
	// Type identifies the problem type. "about:blank" means the status code
	// says everything there is to say.
	Type string `json:"type"`
	// Title is the status text: the same for every occurrence of a type.
	Title string `json:"title"`
	// Status is the HTTP status code.
	Status int `json:"status"`
	// Detail explains this occurrence.
	Detail string `json:"detail,omitempty"`
	// Errors carries per-field validation messages, when there are any.
	Errors []string `json:"errors,omitempty"`
	// Instance identifies this occurrence. kit/httpx sets it to
	// "urn:request:<request id>", which is the same id the log line carries, so
	// a report of "I got a 500" is one grep away from the reason.
	Instance string `json:"instance,omitempty"`
	// contains filtered or unexported fields
}

Problem is an RFC 9457 problem details object.

func Conflict

func Conflict(detail string) *Problem

Conflict is 409: the request contradicts the current state.

func New

func New(status int, detail string) *Problem

New returns a problem with the standard title for status. A status net/http has no text for still gets a title, because a body with an empty one is not an RFC 9457 problem.

func NotFound

func NotFound(detail string) *Problem

New is the constructor for every status; the two below are the only ones with a name of their own, because they are the two kit/rest answers with often enough that spelling the status at each site would be the thing that goes wrong.

NotFound is 404: no such thing, or none this tenant may see.

func (*Problem) ContentType

func (p *Problem) ContentType(string) string

ContentType satisfies huma.ContentTypeFilter, so the response is labelled application/problem+json rather than application/json.

func (*Problem) Error

func (p *Problem) Error() string

Error makes a Problem usable as an error.

func (*Problem) GetStatus

func (p *Problem) GetStatus() int

GetStatus satisfies huma.StatusError, so returning a Problem from a handler sets the response status.

func (*Problem) Unwrap

func (p *Problem) Unwrap() error

Unwrap exposes the server-side cause to logging and errors.Is.

Jump to

Keyboard shortcuts

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