commonhttp

package
v1.0.0-beta.117 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmptyResponseEncoder

func EmptyResponseEncoder[Response any](statusCode int) httptransport.ResponseEncoder[Response]

func ErrorEncoder

func ErrorEncoder(ctx context.Context, _ error, w http.ResponseWriter) bool

ErrorEncoder encodes an error as HTTP 500 Internal Server Error.

func HandleErrorIfTypeMatches

func HandleErrorIfTypeMatches[T error](ctx context.Context, statusCode int, err error, w http.ResponseWriter, extendedProblemFunc ...func(T) (string, string)) bool

HandleErrorIfTypeMatches checks if the error is of the given type and encodes it as an HTTP error. Using the generic feature we can mandate that the error implements the error interface. This is a must, as the errors.As would panic if the error does not implement the error interface.

func JSONRequestBodyDecoder

func JSONRequestBodyDecoder(r *http.Request, out any) error

func JSONResponseEncoder

func JSONResponseEncoder[Response any](_ context.Context, w http.ResponseWriter, response Response) error

JSONResponseEncoder encodes a response as JSON.

func JSONResponseEncoderWithStatus

func JSONResponseEncoderWithStatus[Response any](statusCode int) httptransport.ResponseEncoder[Response]

func PlainTextResponseEncoder

func PlainTextResponseEncoder[Response string](_ context.Context, w http.ResponseWriter, response Response) error

PlainTextResponseEncoder encodes a response as PlainText.

Types

type ErrorWithHTTPStatusCode

type ErrorWithHTTPStatusCode struct {
	StatusCode int
	Extensions []ExtendProblemFunc
	// contains filtered or unexported fields
}

func NewHTTPError

func NewHTTPError(statusCode int, err error, extensions ...ExtendProblemFunc) ErrorWithHTTPStatusCode

func (ErrorWithHTTPStatusCode) EncodeError

type ExtendProblemFunc

type ExtendProblemFunc func() (name string, details string)

func ExtendProblem

func ExtendProblem(name, details string) ExtendProblemFunc

Jump to

Keyboard shortcuts

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