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 ¶
- func ErrorWriter(w http.ResponseWriter, r *http.Request, f cf_http.Failure)
- func Write(w http.ResponseWriter, r *http.Request, p Problem) error
- type Problem
- func BadRequest(title, detail string) Problem
- func Conflict(title, detail string) Problem
- func Forbidden(title, detail string) Problem
- func InternalServerError(title, detail string) Problem
- func MethodNotAllowed(title, detail string) Problem
- func New(status int, title string) Problem
- func NotFound(title, detail string) Problem
- func ServiceUnavailable(title, detail string) Problem
- func TooManyRequests(title, detail string) Problem
- func Unauthorized(title, detail string) Problem
- func (p Problem) MarshalJSON() ([]byte, error)
- func (p Problem) WithDetail(detail string) Problem
- func (p Problem) WithExtension(key string, value any) Problem
- func (p Problem) WithInstance(instance string) Problem
- func (p Problem) WithRequestID(requestID string) Problem
- func (p Problem) WithType(typ string) Problem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorWriter ¶ added in v0.0.9
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.
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 ¶
BadRequest creates a 400 Bad Request problem.
func InternalServerError ¶
InternalServerError creates a 500 Internal Server Error problem.
func MethodNotAllowed ¶
MethodNotAllowed creates a 405 Method Not Allowed problem.
func ServiceUnavailable ¶
ServiceUnavailable creates a 503 Service Unavailable problem.
func TooManyRequests ¶
TooManyRequests creates a 429 Too Many Requests problem.
func Unauthorized ¶
Unauthorized creates a 401 Unauthorized problem.
func (Problem) MarshalJSON ¶
MarshalJSON implements json.Marshaler to support extensions.
func (Problem) WithDetail ¶
WithDetail sets the problem detail.
func (Problem) WithExtension ¶
WithExtension adds an extension field to the response.
func (Problem) WithInstance ¶
WithInstance sets the problem instance URI.
func (Problem) WithRequestID ¶
WithRequestID sets the request ID for correlation.