problems

package module
v0.2.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: MIT Imports: 15 Imported by: 1

README

Problems

Problem Details for HTTP APIs RFC7807

RFC9457 HTTP JSON Problem Details

Simple Usage

problem := problems.New(problems.Instance("/users")).NotFound("user not found")

Bad Request (RFC7807)

if err := validate.Struct(s); err != nil {
    problems.New(problems.Path(req), problems.InvalidParams(err)).BadRequest("Invalid Parameters").JSON(ctx, req.Writer)
	return
}

Bad Request (RFC9457)

if err := validate.Struct(s); err != nil {
    problems.New(problems.Path(req), problems.ValidationErrors(err)).BadRequest("Invalid Parameters").JSON(ctx, req.Writer)
	return
}

Conversion to error

err := problems.New().Unauthorized("password mismatch").Wrap()
problems.Of(context.TODO, "/login", err).JSON(ctx, req.Writer)

Documentation

Index

Constants

View Source
const (
	DefaultType = "about:blank"
)

Variables

This section is empty.

Functions

func Wrap added in v0.1.3

func Wrap(p Problem) error

func WrapError added in v0.1.3

func WrapError(err error) error

func WriteJson

func WriteJson(ctx context.Context, w http.ResponseWriter, status int, v interface{})

func WriteXml

func WriteXml(ctx context.Context, w http.ResponseWriter, status int, v interface{})

Types

type BadRequest

type BadRequest struct {
	*DefaultProblem
	InvalidParams []InvalidParam    `json:"invalid-params,omitempty"`
	Errors        []ValidationError `json:"errors,omitempty"`
}

func (*BadRequest) JSON

func (p *BadRequest) JSON(ctx context.Context, w http.ResponseWriter)

func (*BadRequest) Wrap

func (p *BadRequest) Wrap() error

func (*BadRequest) XML

func (p *BadRequest) XML(ctx context.Context, w http.ResponseWriter)

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func New

func New(f ...Option) *Builder

func (*Builder) BadGateway

func (b *Builder) BadGateway(format string, args ...interface{}) Problem

func (*Builder) BadRequest

func (b *Builder) BadRequest(format string, args ...interface{}) Problem

func (*Builder) Conflict

func (b *Builder) Conflict(format string, args ...interface{}) Problem

func (*Builder) ExpectationFailed

func (b *Builder) ExpectationFailed(format string, args ...interface{}) Problem

func (*Builder) FailedDependency

func (b *Builder) FailedDependency(format string, args ...interface{}) Problem

func (*Builder) Forbidden

func (b *Builder) Forbidden(format string, args ...interface{}) Problem

func (*Builder) GatewayTimeout

func (b *Builder) GatewayTimeout(format string, args ...interface{}) Problem

func (*Builder) Gone

func (b *Builder) Gone(format string, args ...interface{}) Problem

func (*Builder) HTTPVersionNotSupported

func (b *Builder) HTTPVersionNotSupported(format string, args ...interface{}) Problem

func (*Builder) InsufficientStorage

func (b *Builder) InsufficientStorage(format string, args ...interface{}) Problem

func (*Builder) InternalServerError

func (b *Builder) InternalServerError(format string, args ...interface{}) Problem

func (*Builder) LengthRequired

func (b *Builder) LengthRequired(format string, args ...interface{}) Problem

func (*Builder) Locked

func (b *Builder) Locked(format string, args ...interface{}) Problem

func (*Builder) LoopDetected

func (b *Builder) LoopDetected(format string, args ...interface{}) Problem

func (*Builder) MethodNotAllowed

func (b *Builder) MethodNotAllowed(format string, args ...interface{}) Problem

func (*Builder) MisdirectedRequest

func (b *Builder) MisdirectedRequest(format string, args ...interface{}) Problem

func (*Builder) NetworkAuthenticationRequired

func (b *Builder) NetworkAuthenticationRequired(format string, args ...interface{}) Problem

func (*Builder) NotAcceptable

func (b *Builder) NotAcceptable(format string, args ...interface{}) Problem

func (*Builder) NotExtended

func (b *Builder) NotExtended(format string, args ...interface{}) Problem

func (*Builder) NotFound

func (b *Builder) NotFound(format string, args ...interface{}) Problem

func (*Builder) NotImplemented

func (b *Builder) NotImplemented(format string, args ...interface{}) Problem

func (*Builder) PaymentRequired

func (b *Builder) PaymentRequired(format string, args ...interface{}) Problem

func (*Builder) PreconditionFailed

func (b *Builder) PreconditionFailed(format string, args ...interface{}) Problem

func (*Builder) PreconditionRequired

func (b *Builder) PreconditionRequired(format string, args ...interface{}) Problem

func (*Builder) ProxyAuthRequired

func (b *Builder) ProxyAuthRequired(format string, args ...interface{}) Problem

func (*Builder) RequestEntityTooLarge

func (b *Builder) RequestEntityTooLarge(format string, args ...interface{}) Problem

func (*Builder) RequestHeaderFieldsTooLarge

func (b *Builder) RequestHeaderFieldsTooLarge(format string, args ...interface{}) Problem

func (*Builder) RequestTimeout

func (b *Builder) RequestTimeout(format string, args ...interface{}) Problem

func (*Builder) RequestURITooLong

func (b *Builder) RequestURITooLong(format string, args ...interface{}) Problem

func (*Builder) RequestedRangeNotSatisfiable

func (b *Builder) RequestedRangeNotSatisfiable(format string, args ...interface{}) Problem

func (*Builder) Teapot

func (b *Builder) Teapot(format string, args ...interface{}) Problem

func (*Builder) TooEarly

func (b *Builder) TooEarly(format string, args ...interface{}) Problem

func (*Builder) TooManyRequests

func (b *Builder) TooManyRequests(format string, args ...interface{}) Problem

func (*Builder) Type

func (b *Builder) Type(format string, args ...interface{}) *Builder

Type sets the type of the problem

func (*Builder) Unauthorized

func (b *Builder) Unauthorized(format string, args ...interface{}) Problem

func (*Builder) Unavailable

func (b *Builder) Unavailable(format string, args ...interface{}) Problem

func (*Builder) UnavailableForLegalReasons

func (b *Builder) UnavailableForLegalReasons(format string, args ...interface{}) Problem

func (*Builder) UnprocessableEntity

func (b *Builder) UnprocessableEntity(format string, args ...interface{}) Problem

func (*Builder) UnsupportedMediaType

func (b *Builder) UnsupportedMediaType(format string, args ...interface{}) Problem

func (*Builder) UpgradeRequired

func (b *Builder) UpgradeRequired(format string, args ...interface{}) Problem

func (*Builder) VariantAlsoNegotiates

func (b *Builder) VariantAlsoNegotiates(format string, args ...interface{}) Problem

type CodeProblem

type CodeProblem struct {
	*DefaultProblem
	Code string `json:"code"`
}

func (*CodeProblem) JSON

func (p *CodeProblem) JSON(ctx context.Context, w http.ResponseWriter)

func (*CodeProblem) Wrap

func (p *CodeProblem) Wrap() error

func (*CodeProblem) XML

type DefaultParams

type DefaultParams interface {
	SetParams(url, detail string)
	SetType(url string)
	SetTitle(title string)
	SetDetail(detail string)
	SetInstance(instance string)
	Problem
}

type DefaultProblem

type DefaultProblem struct {
	Type     string `json:"type"`
	Title    string `json:"title"`
	Status   int    `json:"status,omitempty"`
	Detail   string `json:"detail,omitempty"`
	Instance string `json:"instance,omitempty"`
}

func NewProblem

func NewProblem(status int) *DefaultProblem

func (*DefaultProblem) JSON

func (*DefaultProblem) ProblemStatus

func (p *DefaultProblem) ProblemStatus() int

func (*DefaultProblem) SetDetail

func (p *DefaultProblem) SetDetail(detail string)

func (*DefaultProblem) SetInstance

func (p *DefaultProblem) SetInstance(instance string)

func (*DefaultProblem) SetParams

func (p *DefaultProblem) SetParams(url, detail string)

func (*DefaultProblem) SetTitle

func (p *DefaultProblem) SetTitle(title string)

func (*DefaultProblem) SetType

func (p *DefaultProblem) SetType(url string)

func (*DefaultProblem) String

func (p *DefaultProblem) String() string

func (*DefaultProblem) Wrap

func (p *DefaultProblem) Wrap() error

func (*DefaultProblem) XML

type InvalidParam

type InvalidParam struct {
	Name   string `json:"name"`
	Reason string `json:"reason"`
}

type MsgFunc

type MsgFunc func() string

type Option

type Option func(p DefaultParams) Problem

func Code

func Code(code string) Option

func Detail

func Detail(detail string) Option

func Instance

func Instance(instance string) Option

func InvalidParams

func InvalidParams(err error, params ...InvalidParam) Option

InvalidParams Create RFC7807-style validation error messages

func Path

func Path(req *http.Request) Option

func Title

func Title(title string) Option

func Type

func Type(format string, args ...interface{}) Option

func ValidationErrors

func ValidationErrors(err error, validErrors ...ValidationError) Option

ValidationErrors Create RFC9457-style validation error messages.

type Problem

type Problem interface {
	ProblemStatus() int
	Wrap() error
	String() string
	Renderer
}

func Bind

func Bind(ctx context.Context, status int, body []byte, f ...func(status int) Problem) (problem Problem, err error)

func Decode

func Decode(ctx context.Context, status int, body io.Reader, f ...func(status int) Problem) (problem Problem, err error)

func Of

func Of(ctx context.Context, path string, err error, f ...MsgFunc) Problem

type ProblemError

type ProblemError struct {
	Path string
	// contains filtered or unexported fields
}

func (*ProblemError) Error

func (err *ProblemError) Error() string

func (*ProblemError) Problem

func (err *ProblemError) Problem() Problem

func (*ProblemError) Unwrap added in v0.1.2

func (err *ProblemError) Unwrap() error

type Renderer

type Renderer interface {
	JSON(ctx context.Context, w http.ResponseWriter)
	XML(ctx context.Context, w http.ResponseWriter)
	Wrap() error
}

type ValidationError

type ValidationError struct {
	Detail  string `json:"detail"`
	Pointer string `json:"pointer"`
}

Jump to

Keyboard shortcuts

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