http

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2020 License: MIT Imports: 5 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

DefaultProblemMatchers is a list of default ProblemMatchers. nolint: gochecknoglobals

Functions

This section is empty.

Types

type ErrorMatcher

type ErrorMatcher func(err error) bool

ErrorMatcher checks if an error matches a certain condition.

type ProblemConverter

type ProblemConverter interface {
	// NewProblem creates a new RFC-7807 Problem from an error.
	// A problem can be any structure that marshals to an RFC-7807 compatible JSON/XML structure.
	NewProblem(ctx context.Context, err error) interface{}
}

ProblemConverter converts an error to a RFC-7807 Problem.

See details at https://tools.ietf.org/html/rfc7807

func NewDefaultProblemConverter added in v0.2.0

func NewDefaultProblemConverter(opts ...ProblemConverterOption) ProblemConverter

NewProblemConverter returns a new ProblemConverter implementation populated with default problem matchers.

func NewProblemConverter

func NewProblemConverter(opts ...ProblemConverterOption) ProblemConverter

NewProblemConverter returns a new ProblemConverter implementation.

Example
problemConverter := NewProblemConverter(
	WithProblemMatchers(
		NewStatusProblemMatcher(http.StatusNotFound, func(err error) bool { return err.Error() == "not found" }),
	),
)

err := errors.New("not found")

problem := problemConverter.NewProblem(context.Background(), err).(*problems.DefaultProblem)

fmt.Println(problem.Status, problem.Detail)
Output:

404 not found

type ProblemConverterOption

type ProblemConverterOption interface {
	// contains filtered or unexported methods
}

ProblemConverterOption configures a ProblemConverter using the functional options paradigm popularized by Rob Pike and Dave Cheney. If you're unfamiliar with this style, see: - https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html - https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.

func SetProblemMatchers added in v0.4.0

func SetProblemMatchers(matchers ...ProblemMatcher) ProblemConverterOption

SetProblemMatchers configures a ProblemConverter to match errors. Matchers override to the existing list of matchers. By default an empty problem is created. If no matchers match an error (or no matchers are configured) an HTTP 500 problem is returned.

If a matcher also implements ProblemConverter it is used instead of the builtin ProblemConverter for creating the problem.

If a matcher also implements StatusProblemMatcher the builtin StatusProblemConverter is used for creating the problem.

func WithProblemConverter

func WithProblemConverter(converter ProblemConverter) ProblemConverterOption

WithProblemConverter configures a ProblemConverter.

func WithProblemMatchers

func WithProblemMatchers(matchers ...ProblemMatcher) ProblemConverterOption

WithProblemMatchers configures a ProblemConverter to match errors. Matchers are appended to the existing list of matchers. By default an empty problem is created. If no matchers match an error (or no matchers are configured) an HTTP 500 problem is returned.

If a matcher also implements ProblemConverter it is used instead of the builtin ProblemConverter for creating the problem.

If a matcher also implements StatusProblemMatcher the builtin StatusProblemConverter is used for creating the problem.

func WithStatusProblemConverter

func WithStatusProblemConverter(converter StatusProblemConverter) ProblemConverterOption

WithStatusProblemConverter configures a StatusProblemConverter.

type ProblemMatcher

type ProblemMatcher interface {
	// MatchError evaluates the predefined set of conditions for err.
	MatchError(err error) bool
}

ProblemMatcher matches an error. A ProblemMatcher usually also implements one of the following interfaces:

- StatusProblemMatcher to indicate an HTTP status code for an error - ProblemConverter if a matched error requires special conversion logic

func NewValidationWithViolationsProblemMatcher

func NewValidationWithViolationsProblemMatcher() ProblemMatcher

NewValidationWithViolationsProblemMatcher returns a problem matcher for validation errors that contain violations. If the returned error matches the following interface, a special validation problem is returned by NewProblem:

type violationError interface {
	Violations() map[string][]string
}

type StatusProblem

type StatusProblem interface {
	ProblemStatus() int
}

StatusProblem is the interface describing a problem with an associated Status code.

type StatusProblemConverter

type StatusProblemConverter interface {
	// NewStatusProblem creates a new RFC-7807 Problem with a status code.
	NewStatusProblem(ctx context.Context, status int, err error) StatusProblem
}

StatusProblemConverter converts an error to a RFC-7807 Problem.

See details at https://tools.ietf.org/html/rfc7807

type StatusProblemMatcher

type StatusProblemMatcher interface {
	ProblemMatcher

	// Status returns the HTTP status code.
	Status() int
}

StatusProblemMatcher matches an error and returns the appropriate status code for it.

func NewStatusProblemMatcher

func NewStatusProblemMatcher(status int, errorMatcher ErrorMatcher) StatusProblemMatcher

NewStatusProblemMatcher returns a new StatusProblemMatcher.

type ValidationProblem

type ValidationProblem struct {
	*problems.DefaultProblem

	Violations map[string][]string `json:"violations"`
}

ValidationProblem describes an RFC-7807 problem with validation violations.

func NewValidationProblem

func NewValidationProblem(details string, violations map[string][]string) *ValidationProblem

NewValidationProblem returns a problem with details and validation errors.

Jump to

Keyboard shortcuts

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