problems

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 12 Imported by: 11

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func WriteResponse

func WriteResponse(ctx context.Context, err error, w http.ResponseWriter, r *http.Request)

WriteResponse, converts the error into a Problem and writes the contents into w. The Problem will be decorated with request information and logged if possible.

Types

type BasicProblem

type BasicProblem struct {
	// Type, a URI reference that identifies the problem type.
	// When dereferenced this should provide human-readable documentation for the
	// problem type. When member is not present it is assumed to be "about:blank".
	Type string `json:"type" format:"url"`

	// Title, a short, human-readable summary of the problem type.
	// This should always be the same value for the same Type.
	Title string `json:"title"`

	// Status, the HTTP status code associated with this problem occurrence.
	Status int `json:"status,omitempty"`

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

	// Instance, a URI reference that identifies the specific resource on which the problem occurred.
	Instance string `json:"instance,omitempty" format:"url"`

	// CorrelationID, an unique identifier for tracing this issue in server logs.
	CorrelationID string `json:"correlationId,omitempty"`
}

BasicProblem, common fields for most Problems. Useful for embedding into for custom problem types.

func (*BasicProblem) DecorateWithRequest

func (problem *BasicProblem) DecorateWithRequest(ctx context.Context, r *http.Request)

func (BasicProblem) Error

func (problem BasicProblem) Error() string

func (BasicProblem) ProblemStatus

func (problem BasicProblem) ProblemStatus() int

func (BasicProblem) ProblemTitle

func (problem BasicProblem) ProblemTitle() string

func (BasicProblem) ProblemType

func (problem BasicProblem) ProblemType() string

type InternalProblem

type InternalProblem struct {
	BasicProblem
	// contains filtered or unexported fields
}

func Internal

func Internal(cause error) InternalProblem

func (InternalProblem) Error

func (problem InternalProblem) Error() string

func (InternalProblem) Format

func (problem InternalProblem) Format(f fmt.State, c rune)

func (InternalProblem) Unwrap

func (problem InternalProblem) Unwrap() error

type Problem

type Problem interface {
	error

	// ProblemType, a URI reference that identifies the problem type.
	// When dereferenced this should provide human-readable documentation for the
	// problem type. When member is not present it is assumed to be "about:blank".
	ProblemType() string

	// ProblemTitle, a short, human-readable summary of the problem type.
	// This should always be the same value for the same Type.
	ProblemTitle() string

	// ProblemStatus, the HTTP status code associated with this problem occurrence.
	// If the problem returns 0, it will be set to http.StatusInternalServerError.
	ProblemStatus() int
}

Problem, basic interface for all errors supporting https://tools.ietf.org/html/rfc7807.

func FromError

func FromError(err error) Problem

FromError, convert an Go error into a Problem. This is a no-op if the supplied error already is a problem. Otherwise the returned error is an InternalProblem (HTTP 500).

func Generic

func Generic(status int) Problem

Generic, returns a generic HTTP-based Problem from a HTTP status code.

type RequestDecoratableProblem

type RequestDecoratableProblem interface {
	Problem

	// DecorateWithRequest, attempt to decorate the Problem with information from the request.
	DecorateWithRequest(ctx context.Context, r *http.Request)
}

type ValidationProblem

type ValidationProblem struct {
	BasicProblem
	Reasons []ValidationReason `json:"reasons,omitempty"`
}

func Validation

func Validation(reasons ...ValidationReason) ValidationProblem

func (*ValidationProblem) Append

func (problem *ValidationProblem) Append(reasons ...ValidationReason)

func (*ValidationProblem) AppendWithPrefix

func (problem *ValidationProblem) AppendWithPrefix(prefix string, reasons ...ValidationReason)

func (ValidationProblem) Error added in v0.13.1

func (problem ValidationProblem) Error() string

func (*ValidationProblem) Errors

func (problem *ValidationProblem) Errors() (errors []error)

func (*ValidationProblem) Merge

func (problem *ValidationProblem) Merge(prefix string, err error)

func (ValidationProblem) TrimEmpty

func (problem ValidationProblem) TrimEmpty() Problem

type ValidationReason

type ValidationReason struct {
	Name   string `json:"name"`
	Reason string `json:"reason"`
	Cause  error  `json:"-"`
}

func (ValidationReason) Error

func (err ValidationReason) Error() string

Jump to

Keyboard shortcuts

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