httpjson

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONError

func JSONError(w http.ResponseWriter, err interface{}, code int)

JSONError is https://pkg.go.dev/net/http#Error, but for JSON.

Types

type Handler

type Handler func(w http.ResponseWriter, r *http.Request) (any, error)

Handler is an HTTP handler implementing http.Handler.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler. If an error is returned from h, it is converted to a JSON error. Otherwise, the returned value is JSON encoded.

type ProblemDetailer

type ProblemDetailer interface {
	ProblemDetail() any
	Code() int
}

ProblemDetailer should be implemented by any error that has should be encoded as a specific RFC 7807 problem detail.

type StatusProblem

type StatusProblem struct {
	Status int    `json:"-"`
	Detail string `json:"detail,omitempty"`
	Err    error
}

StatusProblem is basically an error with an HTTP status code.

func NewInternalProblem

func NewInternalProblem(detail string, err error) *StatusProblem

NewInternalProblem returns a StatusProblem with code 500.

func NewStatusProblem

func NewStatusProblem(code int, detail string, err error) *StatusProblem

NewStatusProblem is a helper to create a StatusProblem.

func (*StatusProblem) Code

func (p *StatusProblem) Code() int

Code returns the HTTP status code of p.

func (*StatusProblem) Error

func (p *StatusProblem) Error() string

func (*StatusProblem) ProblemDetail

func (p *StatusProblem) ProblemDetail() any

ProblemDetail turns p into a RFC 7807 problem detail.

func (*StatusProblem) String

func (p *StatusProblem) String() string

Jump to

Keyboard shortcuts

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