problems

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: MIT Imports: 14 Imported by: 1

README

Problems

Problem Details for HTTP APIs RFC7807

Simple Usage

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

Bad Request

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

Conversion to error

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

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func NewBadRequest

func NewBadRequest(err error, params ...InvalidParam) func(p *DefaultProblem) Problem

func NewCodeProblem

func NewCodeProblem(code string, typ ...string) func(p *DefaultProblem) Problem

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"`
}

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(path string, f ...func(p *DefaultProblem) Problem) *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

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, instance, detail string)
	SetType(url string)
	SetTitle(title string)
	SetDetail(detail string)
	SetInstance(instance string)
}

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, instance, 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 ParamError added in v0.1.4

type ParamError []InvalidParam

func (ParamError) Error added in v0.1.4

func (err ParamError) Error() string

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 ServerProblemOf

func ServerProblemOf(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
}

Jump to

Keyboard shortcuts

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