rfc9457

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 8 Imported by: 0

README

go-rfc9457

Go package for implementing RFC 9457 error responses over HTTP(S)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ResponseArchetype = reflect.TypeOf((*Response)(nil))

Functions

func EnsureLogger

func EnsureLogger() *slog.Logger

func Logger

func Logger() *slog.Logger

func RegisterExtension

func RegisterExtension(ext Extension)

func SetLogger

func SetLogger(l *slog.Logger)

func SprintfMany

func SprintfMany(prefix, sep string, params ...any) (s string)

SprintfMany allows printing many name-value pairs starting with a prefix, e.g

func (r *Response) Error() string {
	return SprintfMany(r.Title, "\n",
		"title=%s", r.Title,
		"error_type=%s", r.Type,
		"error_detail=%s", r.Detail,
		"http_status=%d", r.Status,
		"instance=%s", r.Instance,
		"parameter=%s", r.Parameter,
		"expected_type=%s", r.ExpectedType,
		"received_value=%s", r.ReceivedValue,
		"error_location=%s", r.Location,
		"constraint=%v", r.Constraint,
		"suggestion=%s", r.Suggestion,
		"validation_errors=%v", r.ValidationErrors,
		"received_value=%s", r.ReceivedValue,
	)
}

Types

type ContentGetter

type ContentGetter interface {
	Content() any
}

type ErrorTypeURI

type ErrorTypeURI string
const (
	ErrorTypeRootURI  ErrorTypeURI = "https://schema.xmlui.org/errors"
	TestServerAPIPath ErrorTypeURI = "/test-server/api"
)

Error type URLs (RFC 9457 requires absolute URIs)

const (
	InvalidParameterErrorType    ErrorTypeURI = uri + path + "/validation/invalid-parameter-type"
	ConstraintViolationErrorType ErrorTypeURI = uri + path + "/validation/constraint-violation"
	MissingParametersErrorType   ErrorTypeURI = uri + path + "/validation/missing-required-parameters"
	CurrentlyUnhandledErrorType  ErrorTypeURI = uri + path + "/validation/currently-unhandled"
	UnauthorizedErrorType        ErrorTypeURI = uri + path + "/validation/unauthorized"
	InvalidBodyFormatErrorType   ErrorTypeURI = uri + path + "/validation/invalid-body-format"
	InvalidURLFormatErrorType    ErrorTypeURI = uri + path + "/validation/invalid-url-format"
	InvalidURLParameterErrorType ErrorTypeURI = uri + path + "/validation/invalid-url-parameter"
	InvalidDBQueryErrorType      ErrorTypeURI = uri + path + "/validation/invalid-database-query"
	InternalServerErrorType      ErrorTypeURI = uri + path + "/server/internal"
	EndpointNotMatchedErrorType  ErrorTypeURI = uri + path + "/routing/endpoint-not-matched"
	NoResultsErrorType           ErrorTypeURI = uri + path + "/database/no-results"

	CardinalityMismatchErrorType ErrorTypeURI = uri + path + "/routing/cardinality-mismatch"
	MethodNotAllowedErrorType    ErrorTypeURI = uri + path + "/routing/method-not-allowed"
	QueryFailedErrorType         ErrorTypeURI = uri + path + "/database/query-failed"
)

type Extension

type Extension interface{}

type MIMEType

type MIMEType string
const (
	ApplicationJSON        MIMEType = "application/json"
	ApplicationProblemJSON MIMEType = "application/problem+json"
)

type Response

type Response struct {
	Type       ErrorTypeURI `json:"type"`
	Title      string       `json:"title"`
	Status     int          `json:"status"`
	Detail     string       `json:"detail,omitempty"`
	Instance   string       `json:"instance,omitempty"`
	Extensions []Extension  `json:"extensions,omitempty"`
}

func NewResponse

func NewResponse(args ResponseArgs) *Response

func (*Response) AddExtension

func (r *Response) AddExtension(ext Extension)

func (*Response) Error

func (r *Response) Error() string

func (*Response) HTTPStatusCode

func (r *Response) HTTPStatusCode() int

func (*Response) MIMEType

func (r *Response) MIMEType() MIMEType

func (*Response) ResponsePayload

func (*Response) ResponsePayload()

func (*Response) UnmarshalJSON

func (r *Response) UnmarshalJSON(data []byte) error

func (*Response) Write

func (r *Response) Write(w http.ResponseWriter) error

type ResponseArgs

type ResponseArgs struct {
	Type       ErrorTypeURI `json:"type"`
	Title      string       `json:"title"`
	Status     int          `json:"status"`
	Detail     string       `json:"detail"`
	Instance   string       `json:"instance"`
	Extensions []Extension  `json:"extensions"`
}

func (*ResponseArgs) AddExtension

func (r *ResponseArgs) AddExtension(ext Extension)

type ResponsePayload

type ResponsePayload interface {
	ResponsePayload()
	HTTPStatusCode() int
	MIMEType() MIMEType
	error
}

type Selector

type Selector string

func Selectors

func Selectors[S ~string](ss []S) (ids []Selector)