problem

package
v0.0.11 Latest Latest
Warning

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

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

Documentation

Overview

Package problem provides RFC 9457 Problem Details for HTTP APIs helper.

This package is optional and REST-oriented. It is not used automatically and should not be applied to GraphQL or OAuth responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorWriter added in v0.0.9

func ErrorWriter(w http.ResponseWriter, r *http.Request, f cf_http.Failure)

ErrorWriter is a cf_http.ErrorWriter that serializes Failure as RFC 9457 application/problem+json. Pass it to Recover, CSRF, or MaxBodyBytes:

cf_http.Recover(getLogger, problem.ErrorWriter)
cf_http.MaxBodyBytes(1<<20, problem.ErrorWriter)

Title is Failure.Message (or the status text if Message is empty). The machine code is the JSON extension "code". GraphQL and OAuth handlers should not use this writer — they keep their native envelopes.

func Write

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

Write writes a Problem Details response to the HTTP response writer. The request's correlation ID (RequestIDFrom) is used for request_id when the problem does not already carry one. It returns an error if JSON encoding fails.

Types

type Problem

type Problem struct {
	// Type is a URI reference that identifies the problem type.
	// When this member is not present, its value is assumed to be "about:blank".
	Type string `json:"type,omitempty"`

	// Title is a short, human-readable summary of the problem type.
	// It SHOULD NOT change from occurrence to occurrence of the problem,
	// except for purposes of localization.
	Title string `json:"title,omitempty"`

	// Status is the HTTP status code generated by the origin server for this
	// occurrence of the problem.
	Status int `json:"status,omitempty"`

	// Detail is a human-readable explanation specific to this occurrence of
	// the problem.
	Detail string `json:"detail,omitempty"`

	// Instance is a URI reference that identifies the specific occurrence of
	// the problem.
	Instance string `json:"instance,omitempty"`

	// RequestID is an extension field for request correlation.
	RequestID string `json:"request_id,omitempty"`

	// Extensions allows additional fields to be added to the response.
	Extensions map[string]any `json:"-"`
}

Problem represents an RFC 9457 Problem Details response.

func BadRequest

func BadRequest(title, detail string) Problem

BadRequest creates a 400 Bad Request problem.

func Conflict

func Conflict(title, detail string) Problem

Conflict creates a 409 Conflict problem.

func Forbidden

func Forbidden(title, detail string) Problem

Forbidden creates a 403 Forbidden problem.

func InternalServerError

func InternalServerError(title, detail string) Problem

InternalServerError creates a 500 Internal Server Error problem.

func MethodNotAllowed

func MethodNotAllowed(title, detail string) Problem

MethodNotAllowed creates a 405 Method Not Allowed problem.

func New

func New(status int, title string) Problem

New creates a new Problem Details response with the given status and title.

func NotFound

func NotFound(title, detail string) Problem

NotFound creates a 404 Not Found problem.

func ServiceUnavailable

func ServiceUnavailable(title, detail string) Problem

ServiceUnavailable creates a 503 Service Unavailable problem.

func TooManyRequests

func TooManyRequests(title, detail string) Problem

TooManyRequests creates a 429 Too Many Requests problem.

func Unauthorized

func Unauthorized(title, detail string) Problem

Unauthorized creates a 401 Unauthorized problem.

func (Problem) MarshalJSON

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

MarshalJSON implements json.Marshaler to support extensions.

func (Problem) WithDetail

func (p Problem) WithDetail(detail string) Problem

WithDetail sets the problem detail.

func (Problem) WithExtension

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

WithExtension adds an extension field to the response.

func (Problem) WithInstance

func (p Problem) WithInstance(instance string) Problem

WithInstance sets the problem instance URI.

func (Problem) WithRequestID

func (p Problem) WithRequestID(requestID string) Problem

WithRequestID sets the request ID for correlation.

func (Problem) WithType

func (p Problem) WithType(typ string) Problem

WithType sets the problem type URI.

Jump to

Keyboard shortcuts

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